【问题标题】:ASP.NET MVC 4 Routes not workingASP.NET MVC 4 路由不起作用
【发布时间】:2013-02-23 02:49:42
【问题描述】:

路线

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

我的控制器

 public class HomeController : BaseController
    {
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }

我的 Global.asax

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

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

最后是我的请求网址

http://localhost:1234/Contact/

浏览器出错

找不到资源。

描述:HTTP 404。您正在寻找的资源(或其之一 依赖项)可能已被删除,名称已更改,或者是 暂时不可用。请查看以下 URL 并制作 确保拼写正确。

请求的网址:/Contact/

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.18033

我做错了什么?

解决方案:

自定义路由优先

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

【问题讨论】:

  • 你的默认路由在哪里?
  • 我有,只是这里没有,觉得没必要。
  • 就是,自定义路由要高于默认路由。
  • 哎呀。我的错。刚刚意识到。
  • 很好,你解决了,我会发布它作为答案。

标签: asp.net-mvc-4 routes maproute


【解决方案1】:

框架总是尝试按照添加到RouteCollection的路由的顺序将请求的URL与route匹配

所以你应该把自定义路由放在默认路由之前,

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

【讨论】:

【解决方案2】:
You can use:

routes.MapRoute(
            name: "Default",
            url: "{*p}",
            defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
        );
The asterisk indicates that it's a catch-all route. Keep in mind that these routes are extremely greedy, make sure that this stays below any specific routes.

You could also add a route constraint to this route which can determine whether the page exists in the database or something. 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多