在 ASP.NET MVC 5 中配置 AttributeRouting

Configure AttributeRouting in ASP.NET MVC 5

我有一个区域 RetailersAssistance 和一个控制器-

namespace SkyTracker.Areas.RetailersAssistance.Controllers
{
  [RouteArea("ra")]
  public class LoginController : Controller
  {       
    [Route("foo")]
    public ActionResult Test()
    {
        return View();
    }
 }
}

我想通过 http://localhost:61899/ra/foo 达到 Test() 操作方法。

我已经配置RouteConfig.cs允许属性路由-

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

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

这是我的地区注册码-

public class RetailersAssistanceAreaRegistration : AreaRegistration 
{
    public override string AreaName 
    {
        get 
        {
            return "RetailersAssistance";
        }
    }


    public override void RegisterArea(AreaRegistrationContext context) 
    {

        context.MapRoute(
            "RetailersAssistance_default",
            "ra/{controller}/{action}/{id}",
            new {controller="Login",  action = "Test", id = UrlParameter.Optional },
            new[] { "SkyTracker.Areas.RetailersAssistance.Controllers" }
        );
    }
}

但是上面url returns 404.我在这里错过了什么?

使用 A​​ctionName 而不是 Route 属性。 更改

 [Route("foo")]
 public ActionResult Test()
 {
        return View();
  }

[ActionName("foo")]
public ActionResult Test()
{
   return View();
}

像这样在 RegisterArea 方法中尝试使用 [context.Routes.MapMvcAttributeRoutes()] :

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.Routes.MapMvcAttributeRoutes();
        context.MapRoute(
            "RetailersAssistance_default",
            "ra/{controller}/{action}/{id}",
            new { controller = "Login", action = "Test", id = UrlParameter.Optional },
            new[] { "SkyTracker.Areas.RetailersAssistance.Controllers" }
        );
    }