[英]Using MVC5's RoutePrefix with areas
I currently have a controller which is located at /account/signin.我目前有一个位于 /account/signin 的控制器。 How can I use MVC5's RoutePrefix to make it addressable at /account/sign-in?
如何使用 MVC5 的 RoutePrefix 使其在 /account/sign-in 处可寻址?
I've tried decorating my controller:我试过装饰我的控制器:
[RoutePrefix("account/sign-in")]
public class SignInController : Controller
{
public ActionResult Index()
{
return View();
}
}
and mapping my routes in RegisterRoutes
:并在
RegisterRoutes
中映射我的路线:
routes.MapMvcAttributeRoutes();
but I get this exception when doing so:但是这样做时我得到了这个异常:
The controller for path '/account/sign-in' was not found or does not implement IController
找不到路径“/account/sign-in”的控制器或未实现 IController
I deleted my default routing file ( AccountAreaRegistration.cs
) and it's now working.我删除了我的默认路由文件 (
AccountAreaRegistration.cs
),它现在可以工作了。 I didn't realise you can't use both!我没有意识到你不能同时使用!
You can also add:您还可以添加:
context.Routes.MapMvcAttributeRoutes();
to the RegisterArea
method in your AccountAreaRegistration
class, and then use something like:到
AccountAreaRegistration
类中的RegisterArea
方法,然后使用类似:
[RouteArea("admin")]
[RoutePrefix("menu")]
[Route("{action=index}/{id?}")]
At the top of your controller, use attribute routing for one or more controllers in your area and leave the rest using the default you set up;在控制器的顶部,为您所在区域的一个或多个控制器使用属性路由,其余的使用您设置的默认值; eg,
例如,
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.MapMvcAttributeRoutes();
context.MapRoute(
name: "Admin",
url: "Admin/{controller}/{action}/{id}",
defaults: new { area = "Admin", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Then you have both.那么你两者都有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.