简体   繁体   中英

Using MVC5's RoutePrefix with areas

I currently have a controller which is located at /account/signin. How can I use MVC5's RoutePrefix to make it addressable at /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 :

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

I deleted my default routing file ( AccountAreaRegistration.cs ) and it's now working. 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:

[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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM