【问题标题】:Default Route does not work after upgrading to MVC5 with Attribute Routing使用属性路由升级到 MVC5 后默认路由不起作用
【发布时间】:2015-01-15 22:54:12
【问题描述】:

从带有 AttributeRouting.net 的 MVC4 升级到带有 MVC5 属性路由的 MVC5 后,我似乎无法让默认路由正常工作,因此 http://server 默认为 http://server/home/index 。直接浏览/home//home/index 可以正常工作。

对于路由配置,我有这个:

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

HomeController 声明如下所示:

// Controller
[RoutePrefix("home")]
public class HomeController : MvcControllerBase
....
    // Action
    [HttpGet, Route, Route("index")]
    public ActionResult Index()
    {
   .....

我不知道还能去哪里检查。我已注释掉 Global 中的所有内容并禁用了所有 WebActivator 激活的项目。

还有什么想法?响应为 404,没有抛出异常。

【问题讨论】:

    标签: attributerouting asp.net-mvc-5.2


    【解决方案1】:

    啊..明白了!

    基于 Kiran 的回答:Specify default controller/action route in WebAPI using AttributeRouting

    我将 HomeController 更改为:

    // Controller
    public class HomeController : MvcControllerBase
    ....
        // Action
        [HttpGet, Route, Route("home"), Route("home/index")]
        public ActionResult Index()
        {
    ....
    

    我摆脱了 MVC 配置默认值。

    【讨论】:

      猜你喜欢
      • 2014-12-27
      • 2016-01-06
      • 1970-01-01
      • 2019-05-12
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 2010-11-19
      相关资源
      最近更新 更多