asp.net-mvc-routing - ASP.NET MVC 6 中默认 url 的路由错误

标签 asp.net-mvc-routing asp.net-core-mvc asp.net-core-1.0

我在 MVC 6 Web 应用程序中遇到路由问题:当我在默认使用的 Controller 中设置路由参数时,应用程序发送 404 错误。

我的路由配置:

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Dashboard}/{action=Index}/{id?}");
        });

我的仪表板 Controller (应用程序工作):

[Authorize]
public class DashboardController : Controller
{
     public DashboardController()
     { }

     [HttpGet]
     public IActionResult Index() => View(new IndexViewModel());
}

相同的仪表板 Controller (应用程序响应 404 错误):

[Authorize]
[Route("[controller]")]
public class DashboardController : Controller
{
     public DashboardController()
     { }

     [HttpGet]
     [Route("[action]")]
     public IActionResult Index() => View(new IndexViewModel());
}

最佳答案

发生这种情况的原因是通过routes.MapRoute指定的路由仅适用于使用基于属性的路由的 Controller 。由于您的第二个示例使用基于属性的路由,因此只能通过属性中指定的路由到达 Controller 。因此只能在/Dashboard/Index 处访问

关于asp.net-mvc-routing - ASP.NET MVC 6 中默认 url 的路由错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36938106/

相关文章:

c# - 属性路由 - 强类型操作链接

c# - 路由错误访问 Action

c# - .NET 核心 : How to get a `Controller` instance (or just it's full name) from ViewLocationExpanderContext

c# - MVC 6 多个获取方法

asp.net-core - ASP.Net Core MVC中的DataAnnotationsModelValidatorProvider.RegisterAdapter

asp.net-core - 在 .NET Core OpenID Connect 中间件中重定向?

asp.net-core-1.0 - ASP.NET Core 1 Web API 模型绑定(bind)数组

c# - ASP.NET MVC - 创建没有后备的自定义路由

linux - 如何在 AWS Linux 实例上运行 .NET Core MVC 站点

asp.net-mvc - MVC Preview 4 - 路由表中没有路由与提供的值匹配