Routing with and without controller name in ASP.NET MVC 4

This should do it: routes.MapRoute( name: “About”, url: “About”, defaults: new { controller = “Public”, action = “About” } ); routes.MapRoute( name: “MyPageSummary”, url: “MyPage”, defaults: new { controller = “MyPage”, action = “Summary” } ); routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Public”, action = “Start”, id = UrlParameter.Optional } );

How can I create a route constraint of type System.Guid?

Create a RouteConstraint like the following: public class GuidConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { if (values.ContainsKey(parameterName)) { string stringValue = values[parameterName] as string; if (!string.IsNullOrEmpty(stringValue)) { Guid guidValue; return Guid.TryParse(stringValue, out guidValue) && (guidValue != Guid.Empty); } } return false; }} Next when adding … Read more

MVC MapPageRoute and ActionLink

My guess is that you need to add some parameter options to the MapPageRoute declaration. So if you have more than one webforms page in the WebForms directory this works well. routes.MapPageRoute (“ReportTest”, “reports/{pagename}”, “~/WebForms/{pagename}.aspx”); PS: You may also want to have a look at the RouteExistingFiles property of RouteCollection An alternative would be to … Read more

MVC 2 AreaRegistration Routes Order

Aside from what Haacked said, it is very much possible to order area registrations (and thus their routes). All you have to do is register each area manually, in whatever order you want. It’s not as sleek as calling RegisterAllAreas() but it’s definitely doable. protected void Application_Start() { var area1reg = new Area1AreaRegistration(); var area1context … Read more

How to achieve a dynamic controller and action method in ASP.NET MVC?

Absolutely! You’ll need to override the DefaultControllerFactory to find a custom controller if one doesn’t exist. Then you’ll need to write an IActionInvoker to handle dynamic action names. Your controller factory will look something like: public class DynamicControllerFactory : DefaultControllerFactory { private readonly IServiceLocator _Locator; public DynamicControllerFactory(IServiceLocator locator) { _Locator = locator; } protected override … Read more

Routing in Asp.net Mvc 4 and Web Api

You could have a couple of routes: public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: “ApiById”, routeTemplate: “api/{controller}/{id}”, defaults: new { id = RouteParameter.Optional }, constraints: new { id = @”^[0-9]+$” } ); config.Routes.MapHttpRoute( name: “ApiByName”, routeTemplate: “api/{controller}/{action}/{name}”, defaults: null, constraints: new { name = @”^[a-z]+$” } ); config.Routes.MapHttpRoute( name: … Read more

Attribute Routing not working in areas

You are probably combining convention based routing with attribute routing, and you should register your areas after you map the attribute routes. The line AreaRegistration.RegisterAllAreas(); should be called AFTER this line: routes.MapMvcAttributeRoutes(); The explanation (from https://devblogs.microsoft.com/aspnet/attribute-routing-in-asp-net-mvc-5/): If you are using both Areas with route attributes, and areas with convention based routes (set by an AreaRegistration … Read more

How to reuse Areas, Controllers, Views, Models, Routes in multiple apps or websites

Areas cannot be reused the way you think (using virtual directories). They are just an inseparable part of the root web application. They simply embed into the root application routes and cannot function without it. By pointing a virtual directory to your Areas folder you are indicating that this is a separate ASP.NET application but … Read more

Route with Two optional parameters in MVC3 not working

You’re missing the parameters in your route config. In order to make this work with different parameters optional (as in Phil Haack’s post), you need to define multiple routes routes.MapRoute(“UserDetail-WithStatus”, “UserDetail/{id}/{inSaveAction}/{status}”, new { controller = “Admin”, action = “UserDetail”, // nothing optional } ); routes.MapRoute(“UserDetail-WithoutStatus”, “UserDetail/{id}/{inSaveAction}”, new { controller = “Admin”, action = “UserDetail”, // … Read more