【问题标题】:Special MVC Routing not working特殊 MVC 路由不起作用
【发布时间】:2017-02-16 10:02:31
【问题描述】:

我的 MVC 项目中的路由出现问题...

我希望Views > Shared 文件夹中的所有视图都像这样:

Error.cshtml(默认)
Index.cshtml(默认)
Overview.cshtml(我自定义的)
Recordings.cshtml(我定制的)

然后我创建了一个共享控制器来处理所有视图,如下所示:

public class SharedController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Error()
    {
        return View();
    }

    public ActionResult Overview()
    {
        return View();
    }

    public ActionResult Recordings()
    {
        return View();
    }
}

我的RouteConfig.cs 看起来像这样:

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

    // Map to specific pages under Shared controller:
    routes.MapRoute("SharedPages", "{action}/{id}",
        new { controller = "Shared", action = @"Overview|Recordings", id = UrlParameter.Optional });

    // Use the default rout for all other pages:
    routes.MapRoute("Default", "{controller}/{action}/{id}",
        new { controller = "Shared", action = "Index", id = UrlParameter.Optional }
    );

    // Show the Error page for anything else (404):
    routes.MapRoute("Error", "{*url}",
        new { controller = "Shared", action = "Error" }
    );
}

我希望路由像这样工作:

://(url)/(root - 未指定操作)--> Shared/Index.cshtml
://(url)/Index --> Shared/Index.cshtml
://(url)/Overview --> Shared/Overview.cshtml
://(url)/Recordings --> Shared/Recordings.cshtml
://(url)/whatever(或者如果发生错误)--> Shared/Error.cshtml

但它没有按预期工作。如果我去://(url)/(root),我会得到一个HTTP 404 - The resource cannot be found. 如果我去例如://(url)/Overview,它工作正常。

我怎样才能让它像我想要的那样工作?

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-routing


【解决方案1】:

您映射路线的顺序很重要,首先匹配的路线获胜。这意味着即使那里没有资源,它也会匹配它将使用它的路线。

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

    // Use specific rout for all other pages:
    routes.MapRoute("WhateverA", "WhateverA/{action}/{id}",
        new { controller = "WhateverA", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute("WhateverB", "WhateverB/{action}/{id}",
        new { controller = "WhateverB", action = "Index", id = UrlParameter.Optional }
    );

    // Map to specific pages under Shared controller:
    routes.MapRoute("RootPages", "{action}/{id}",
        new { controller = "Shared", action = "Index", id = UrlParameter.Optional });

    // Show the Error page for anything else (404):
    routes.MapRoute("Error", "{*url}",
        new { controller = "Shared", action = "Error" }
    );
}

DefaultSharedPages 路由的问题在于它们相互冲突。如果存在其他控制器,您可能需要为其他控制器提供特定路由。否则,另一个选项是对其他控制器使用属性路由,对根路由和错误使用基于约定的路由

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

    //Attribute routing
    routes.MapMvcAttributeRoutes();

    // Map to specific pages under Shared controller:
    routes.MapRoute("RootPages", "{action}/{id}",
        new { controller = "Shared", action = "Index", id = UrlParameter.Optional });

    // Show the Error page for anything else (404):
    routes.MapRoute("Error", "{*url}",
        new { controller = "Shared", action = "Error" }
    );
}

相应地装饰控制器

[RoutePrefix("WhatEver")]
public class WhatEverController : Controller {
    //GET whatever
    [HttpGet]
    [Route("")]
    public ActionResult Index() { ... }
}

【讨论】:

  • url 中包含 0、1、2 或 3 段的任何内容都将匹配 Default 并且永远不会命中 SharedPages 路由
  • @StephenMuecke,是的,因为默认设置。接得好。将不得不重新查看这个答案。
  • 如果OP想省略控制器名称,那么我每个方法都需要一个特定的路由,或者路由约束(假设还有其他控制器)
  • 我不得不为每个控制器使用特定的路由来解决类似的情况。幸运的是,该项目没有很多控制器。我正要建议
猜你喜欢
  • 1970-01-01
  • 2017-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
  • 2013-02-23
  • 2018-03-11
相关资源
最近更新 更多