Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - MapRoute versus routes.Add (and 404s)

I'm just getting started with ASP.NET MVC.

What is the difference between MapRoute and routes.Add ? Should I just be using MapRoute? Can I map multiple routes? Which "maps" take precedence... those you called first or last?

I'd like to be able to do something similiar to the StackOverflow does for users. But I would like the URL to fit this pattern:
"User/{domain}/{username}" to be routed to a UserController

and for all other requests to do the typical ASP.NET MVC routing. ex:

        routes.MapRoute(
            "Default", "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = "" }  
        );

UPDATE:
When using the URL: http://localhost:3962/User/MYDOMAIN/BTYNDALL
I get the error: 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.

Here is the code I'm using:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "User",                                                     
            "User/{domain}/{username}",                           
            new { controller = "User", action = "Index" }      
        );

        routes.MapRoute(
            "Default",                                              
            "{controller}/{action}/{id}",                           
            new { controller = "Home", action = "Index", id = "" }  
        );

    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}
like image 985
BuddyJoe Avatar asked Feb 04 '09 22:02

BuddyJoe


3 Answers

MapRoute() is an extension method over Routes.Add(). Use MapRoute(), unless you need to do something more complex than it allows.

Routes are evaluated in the order they are defined, so those you called first.

like image 87
Brannon Avatar answered Sep 28 '22 03:09

Brannon


Your User controller should have

public class UserController : Controller {
    public ActionResult Index(string domain, string username) { return View(); }
}

The two variables on the Index method of the user controller get picked up from the route.

like image 24
JMS Avatar answered Sep 28 '22 04:09

JMS


Use!

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "User",                                                     
            "User/{domain}/{username}",                           
            new { controller = "User", action = "Index", username= UrlParameter.Optional }      
        );

       }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}
like image 40
Fabio Avatar answered Sep 28 '22 05:09

Fabio