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

萝らか妹 提交于 2019-12-02 20:32:40

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.

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.

Fabio

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);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!