I have a custom routing class which I added to the RouteConfig
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new CustomRouting());
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
The CustomRouting class looks like this:
public class CustomRouting : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var requestUrl = httpContext.Request?.Url;
if (requestUrl != null && requestUrl.LocalPath.StartsWith("/custom"))
{
if (httpContext.Request?.HttpMethod != "GET")
{
// CustomRouting should handle GET requests only
return null;
}
// Custom rules
// ...
}
return null;
}
}
Essentially I want to process requests that go to the /custom/* path with my custom rules.
But: Requests that are not "GET", should not be processed with my custom rules. Instead, I want to remove the /custom segment at the beginning of the path and then let MVC continue with the rest of the routing configured in RouteConfig.
How can I achieve that?
You can start by filtering "custom" prefixed requests in an HttpModule
HTTP Handlers and HTTP Modules Overview
Example :
public class CustomRouteHttpModule : IHttpModule
{
private const string customPrefix = "/custom";
public void Init(HttpApplication context)
{
context.BeginRequest += BeginRequest;
}
private void BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
if (context.Request.RawUrl.ToLower().StartsWith(customPrefix)
&& string.Compare(context.Request.HttpMethod, "GET", true) == 0)
{
var urlWithoutCustom = context.Request.RawUrl.Substring(customPrefix.Length);
context.RewritePath(urlWithoutCustom);
}
}
public void Dispose()
{
}
}
Then you can have your routes for "custom" urls
routes.MapRoute(
name: "Default",
url: "custom/{action}/{id}",
defaults: new { controller = "Custom", action = "Index", id = UrlParameter.Optional }
);
Note: Don't forget to register your HttpModule in your web.config
<system.webServer>
<modules>
<add type="MvcApplication.Modules.CustomRouteHttpModule" name="CustomRouteModule" />
</modules>
</system.webServer>
Related
So I have ASP.NET MVC application. I would like to configure its routes. Here is my RouteConfig's code:
public static void Register(RouteCollection routes, bool useAttributes = true)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
This route works fine. Besides I have an area in my application and try to configure its routes too. It is my area registration code:
public override void RegisterArea(AreaRegistrationContext context)
{
try
{
context.MapRoute(
name: "SiteSettings_Controller",
url: "SiteSettings/{controller}/{action}/{id}",
defaults: new {action = "Index", id = UrlParameter.Optional,
// here I tried to use #"(UserManagement|Tools|Settings)"
//as constraint but it takes no effect
constraints: new {controller = "UserManagement|Tools|Settings" }
);
}
catch (Exception e)
{
// here I get InvalidOperationException ""
}
}
I would like to restrict controllers in SiteSettingsArea's route but when I go to "localhost/SiteSettings/UserManagement" url I get InvalidOperationException with message "No route in the route table matches the supplied values". I believe that this url corresponds to SiteSettings_Controller route but obviously I am wrong. How could I limit controllers in the route properly?
If you search your codebase for SiteSettings_Controller does it appear anywhere else?
The below code certainly worked for me when I just tested it.
using System;
using System.Web.Mvc;
namespace WebApplication1.Areas.SiteSettings
{
public class SiteSettingsAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "SiteSettings";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "SiteSettings_Controller",
url: "SiteSettings/{controller}/{action}/{id}",
defaults: new
{
action = "Index",
id = UrlParameter.Optional
},
constraints: new { controller = "UserManagement|Tools|Settings" }
);
}
}
}
I have created an mvc application with SSO. I have added an Api controller, but whenever i try to get data from it, i get error.
URI= https://localhost:44305/api/graph/get
Error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /api/graph/get
Here is my Route config
public class RouteConfig
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "api/{controller}/{action}/{id}",
defaults: new { controller = "Graph", action = "Get", id = UrlParameter.Optional }
);
}
}
This is my web api controller
public JsonResult<List<Claim>> Get()
{
var principal = (User as ClaimsPrincipal);
List<Claim> claimsList = new List<Claim>();
foreach (var claim in principal.Claims)
{
claimsList.Add(claim);
}
return Json<List<Claim>>(claimsList);
}
Not getting any build error. Any help is appreciated
Hy Guys.Thanks for all your help. I have figured out the solution. The answer is that in Global.asax.cs,
GlobalConfiguration.Configure(WebApiConfig.Register)
has to be called before
RouteConfig.RegisterRoutes(RouteTable.Routes).
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultDataApi",
routeTemplate: "Api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
FluentValidationModelValidatorProvider.Configure();
}
}
You should have 2 config files:
RoutesConfig.cs and WebApiConfig.cs
RoutesConfig should look like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
Log.Instance().Debug("Registering Routes");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
}
WebApiConfig.cs should look like this:
public static class WebApiConfig
{
public static void Configure(HttpConfiguration config)
{
ConfigureRouting(config);
ConfigureFormatters(config);
}
private static void ConfigureFormatters(HttpConfiguration config)
{
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new NiceEnumConverter());
}
private static void ConfigureRouting(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional, #namespace = "api" });
}
}
Within global.asax.cs you should have something like this:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// Register them in this order
GlobalConfiguration.Configure(WebApiConfig.Configure); //registers WebApi routes
RouteConfig.RegisterRoutes(RouteTable.Routes); //register MVC routes
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
Your controller does look a little strange why not let the default serialiser take care for things for you:
public IEnumerable<Claim> Get()
{
var principal = (User as ClaimsPrincipal);
List<Claim> claimsList = new List<Claim>();
foreach (var claim in principal.Claims)
{
claimsList.Add(claim);
}
return claimsList;
}
EDIT: removed redundant code
Assuming the name of your controller is GraphController, I think the url you should be using is /api/graph/get.
If it's named something different, you'll need to adjust the url accordingly.
Also, there's a separate .cs file in MVC projects you should use to manage WebAPI routes - WebApiConfig.cs
I've been reading a lot about this problem, and I can't figure this out.
Everything is pretty straightforward with routing and ASP .NET MVC, but I'm stuck with this.
The problem is that I'm trying to make a GET to a given url with this form:
{area}/{controller}/{action}
But the {area} is not being registered. My default route is not working either (not working in the sense that I need to go to localhost:port/Home instead of just going to localhost:port/
This is my code:
RouteConfig:
public class RouteConfig
{
public static void RegisterRoute(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
route.MapRoute(
"Default",
"{controller}/{action}",
new {controller = "Home", action = "Index"}
);
}
}
This is the Area that is not being registered:
public class TransaccionesAreaRegistration : AreaRegistration
{
public override string AreaName
{
get{
return “Transacciones”;
}
}
public override void RegisterArea(AreaRegistrationContext context){
context.MapRoute(
"Transacciones_default",
"Transacciones/{controller}/{action}/{id}",
new { controller = "Transacciones", action = "Index", id = UrlParameter.Option}
);
}
}
Finally, this is my global.asax (I do call AreaRegistration.RegisterAllAreas() method):
protected void Application_Start(){
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
I will really appreciate some advice with this, I think I have spent enough time googling :O)
Just try this
RouteConfig:
public class RouteConfig
{
public static void RegisterRoute(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
route.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
This is the Area that is not being registered:
public class TransaccionesAreaRegistration : AreaRegistration
{
public override string AreaName
{
get{
return “Transacciones”;
}
}
public override void RegisterArea(AreaRegistrationContext context){
context.MapRoute(
“Transacciones_default”,
“Transacciones/{controller}/{action}/{id}”,
new { action = ”Index”, id = UrlParameter.Optional },
new string[] { "MyApp.Transacciones.Controllers" } // specify the new namespace
);
}
}
------------------------------OR Try This--------------------------------
public class RouteConfig
{
public static void RegisterRoute(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
AreaRegistration.RegisterAllAreas();
route.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Reason why default route is not working
Because you never registered a default one. Add this line in the RouteConfig -
routes.MapRoute("Home", "", new { Controller = "Home", Action = "Index" });
So the final code should look like this -
public class RouteConfig
{
public static void RegisterRoute(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.MapRoute("Home", "", new { Controller = "Home", Action = "Index" });
route.MapRoute(
"Default",
"{controller}/{action}",
new {controller = "Home", action = "Index"}
);
}
}
Possible reason why Area seems not working
May be the same reason also the Area registration seems not working. Try adding the following line in area registration -
routes.MapRoute("Transacciones_Home", "Transacciones", new { Controller = "Transacciones", Action = "Index" });
So it looks like -
public class TransaccionesAreaRegistration : AreaRegistration
{
public override string AreaName
{
get{
return “Transacciones”;
}
}
public override void RegisterArea(AreaRegistrationContext context){
routes.MapRoute("Transacciones_Home", "Transacciones", new { Controller = "Transacciones", Action = "Index" });
context.MapRoute(
“Transacciones_default”,
“Transacciones/{controller}/{action}/{id}”,
new { controller = “Transacciones”, action = ”Index”, id = UrlParameter.Option}
);
}
}
}
This questions was the one that helped me.
The thing is, the order in the routes' registration is very important. Considering that, I started checking my other areas registration, and I found out that all the requests where falling into the first rule that was a general rule like this:
routes.MapRoute(
name : "Default",
url : {controller}{action}{id}
);
So, after that rule, none of the rules were being considered.
Thanks everyone for trying to help, bests!
This is my goal:
I need to two (or more) "Areas" for my MVC web app. They would be accessed like so:
/* Home */
http://example.com/
http://example.com/about
http://example.com/faq
http://example.com/contact
/* Admin */
http://example.com/admin
http://example.com/admin/login
http://example.com/admin/account
http://example.com/admin/ect
I would like to organize the project like the following:
MyExampleMVC2AreasProject/
Areas/
Admin/
Controllers/
Models/
Views/
Home/
Shared/
Site.Master
Web.Config
AdminAreaRegistration.cs
Web/
Controllers/
Models/
Views/
Home/
Shared/
Site.Master
Web.Config
WebAreaRegistration.cs
Global.asax
Web.Config
So, in Global.asax I have:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
Here is WebAreaRegistration.cs
using System.Web.Mvc;
namespace MyExampleMVC2AreasProject.Areas.Web
{
public class WebAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Web";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"WebDefault",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
'AdminAreadRegistration.cs' is set up the same but the url param is Admin/{action}/{id}.
With the setup above the Web "Area" works great (example.com/about, example.com/contact, etc).
What do I need to do to get the Admin "Area" hooked up with the routes the way I want them? I just get 404ed now.
I've tried every combination of routes, routes w/namespaces, URL Parameters, parameter defaults, etc, I could think of. I have a feeling I'm missing something pretty basic.
I use this AreaRegistrationUtil class. It automatically registers anything which inherits AreaRegistration in any assembly you specify. As an added bonus, it's WAY faster than AreaRegistration.RegisterAllAreas because it only looks at the assembly you specify.
You probably need to set your namespaces on all your area registrations.
Example
context.MapRoute(
"Admin_default",
"admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "MyExampleMVC2AreasProject.Areas.Admin.Controllers" } // This is area namespace
);
My current solution is here: http://notesforit.blogspot.com/2010/08/default-area-mvc-2.html
I don't like it and would love to get a better solution.
-- copied from URL above:
Implement your Areas as usually, register any routes that you need.
For example:
public class PublicAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Public";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Public_default",
"Public/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional, }
);
}
}
And:
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new {controller = "Overview", action = "Index", id = UrlParameter.Optional }
);
}
}
It's important, that URL should have any prefix, for example http://site.com/PREFIX/{controller}/{action}, because prefix of default Area will be cropped
Next in Global.asax.cs:
public class MvcApplication : System.Web.HttpApplication
{
public static string _defaultAreaKey = "DefaultArea";
public static void RegisterDefaultRoute(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//reading parameter DefaultArea from web.config
string defaultArea = ConfigurationManager.AppSettings[_defaultAreaKey];
//check for null
if (String.IsNullOrEmpty(defaultArea))
throw new Exception("Default area isn\'t set");
//select routes from registered,
//which have DataTokens["area"] == DefaultArea
//Note, each Area might have more than one route
var defRoutes = from Route route in routes
where
route.DataTokens != null &&
route.DataTokens["area"] != null &&
route.DataTokens["area"].ToString() == defaultArea
select route;
//cast to array, for LINQ query will be done,
//because we will change collection in cycle
foreach (var route in defRoutes.ToArray())
{
//Important! remove from routes' table
routes.Remove(route);
//crop url to first slash, ("Public/", "Admin/" etc.)
route.Url = route.Url.Substring(route.Url.IndexOf("/") + 1);
//Important! add to the End of the routes' table
routes.Add(route);
}
}
protected void Application_Start()
{
//register all routes
AreaRegistration.RegisterAllAreas();
//register default route and move it to end of table
RegisterDefaultRoute(RouteTable.Routes);
}
}
Do not forget add parameter to web.config:
<configuration>
<appSettings>
<add key="DefaultArea" value="Public"/>
</appSettings>
<!-- ALL OTHER-->
</configuration>
My folder look like this:
(root)/Areas/Admin/Views/..
(root)/Areas/Admin/Controllers/...
(root)/Areas/Admin/Routes.cs
(root)/Areas/Forum/Views/..
(root)/Areas/Forum/Controllers/...
(root)/Areas/Forum/Routes.cs
public class Routes : AreaRegistration
{
public override string AreaName
{
get { return "Admin"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_Default",
"{controller}/{action}/{Id}",
new { controller = "Admin", action = "Index", Id = (string)null }
);
}
}
public class Routes : AreaRegistration
{
public override string AreaName
{
get { return "Forum"; }
}
public override void RegisterArea(AreaRegistrationContext routes)
{
routes.MapRoute(
"Forum_Default",
"{controller}/{action}",
new { controller = "Forum", action = "Index"}
);
}
}
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The startpage should be Home/Index but it start with Admin/Index, why?
Only site.com/Admin works not site.com/Forum
How should i get Admin and Forum Areas to work right? Why is only Admin working and not Forum?
When i delete Admin/Routes.cs file Forum start to work...
EDIT:
Home in ~/Views/ don't show as startpage even if i have
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
in my Global.asax after AreaRegistration.RegisterAllAreas();
I believe your area mappings should be structured like so.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_Default",
"Admin/{controller}/{action}/{Id}",
new { controller = "Admin", action = "Index", Id = (string)null }
);
}
and
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Forum_Default",
"Forum/{controller}/{action}/{Id}",
new { controller = "Forum", action = "Index"}
);
}
Keeps your routes from conflicting, which is what i think is happening in your case. As your default route matches your admin route.