属性路由无法正常工作,并且指定的URL返回HTTP 404找不到?

时间:2019-01-11 08:22:55

标签: c# asp.net-mvc

关于此 Attibute路由,当我运行应用程序时,我收到HTTP 404 Not Found结果为什么?

[Route("custom/route/{id}/{name}")]
public ActionResult Student(int id, string name)
{
   return Content(string.Format("id = {0} & name = {1}", id, name)); 
}

结果:

enter image description here

1 个答案:

答案 0 :(得分:0)

确保您的RouteConfig.cs看起来像这样:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection Routes)
    {
        Routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
        Routes.MapMvcAttributeRoutes();

        Routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            namespaces: new string[] { "YourControllerNamespace.Controllers" },
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );           
    }
}

请注意 Routes.MapRoute

中的命名空间参数