2

To define your own URL routes in ASP.NET MVC4, I believe you modify Global.asax.cs so it looks something like...

using System.Web.Mvc; 
using System.Web.Routing; 
namespace MvcApplication1 { 
    public class MvcApplication : System.Web.HttpApplication { 
        public static void RegisterRoutes(RouteCollection routes) { 
            routes.MapRoute( .....

But what I cant find anywhere, is how you add multiple routes - e.g. do you just call routes.MapRoute(....) again?

Also, does this overwrite the hidden default routing definitions? If I want to keep it do I need to define it also?

2
  • to add routes check this msdn.microsoft.com/en-us/library/… Commented Aug 11, 2013 at 15:57
  • 1
    You tagged this as asp.net-mvc-4 so I'd recommend using the MVC4 way and wire up your routes in the /App_Start/RouteConfig.cs file. Commented Aug 11, 2013 at 17:41

3 Answers 3

3

I just want add to Embram's answer that the best practice is to add routes from most detailed to general:

routes.MapRoute(
                name: "Route1",
                url: "Mail/{id}",
                defaults: new { controller = "Home", action = "Mail", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Route2",
                url: "{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
Sign up to request clarification or add additional context in comments.

Comments

2

To add multiple routes, kindly check this adding_routes_to_an_mvc_application. As you said just make call routes.MapRoute(....) again.

The one thing to put into consideration MVC Routing respect ordering route. Your last route must be generic as possible, and your previous route must be specific as possible.(check this out ASP.NET MVC Default route)

does this overwrite the hidden default routing definitions?

I don't think so, but your reoutes sure 'll be checked first before the default MVC routes.(check this SO post)

Comments

1

It seems you're confused on where the routes are defined in an MVC 4 application.

You're global.asax should look like (by default):

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }
}

Where RouteConfig is defined in /App_Start/RouteConfig.cs as looks like (by default):

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Knowing this, you should be able to edit the routes as needed.

1 Comment

Thanks, that make more sense. The MSDN tutorial I was was reading may be a bit out of date.... asp.net/mvc/tutorials/controllers-and-routing/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.