ASP.NET MVC - MapRoute 与 routes.Add(和 404s)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/513663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 11:11:16  来源:igfitidea点击:

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

.netasp.net-mvcurl-routingasp.net-mvc-routing

提问by BuddyJoe

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

我刚刚开始使用 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?

MapRoute 和 routes.Add 有什么区别?我应该只使用 MapRoute 吗?我可以映射多条路线吗?哪些“地图”优先……您首先调用的还是最后调用的那些?

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

我希望能够做一些类似于 StackOverflow 为用户所做的事情。但我希望 URL 适合这种模式:
“User/{domain}/{username}” 被路由到 UserController

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

以及执行典型 ASP.NET MVC 路由的所有其他请求。前任:

        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.

更新:
使用 URL 时:http://localhost:3962/User/MYDOMAIN/BTYNDALL
我收到错误:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除,名称已更改,或暂时不可用。

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);
    }
}

采纳答案by JMS

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.

用户控制器的 Index 方法上的两个变量从路由中获取。

回答by Brannon

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

MapRoute()是一种扩展方法Routes.Add()。使用MapRoute(), 除非你需要做一些比它允许的更复杂的事情。

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

路由按照定义的顺序进行评估,所以你首先调用的那些。

回答by 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);
    }
}