Controllers split by areas [duplicate]

我只是一个虾纸丫 提交于 2019-11-28 07:48:35

My Suggestion would be to implement a custom DefaultControllerFactory.

You can see a very good example here

The default controller factory just lists all controllers by name on a list not allowing for this kind of functionality. The article above shows you how to create a new factory and take control over the controller creation allowing you to easily match routes to specific namespace's.

That would give the functionality you are looking for.

Try adding the following using statement and modifying the route registration in your AreaRegistration.cs file.

using System.Web.Http;
...
public override void RegisterArea(AreaRegistrationContext context)
{
        context.Routes.MapHttpRoute(
            name: this.AreaName,
            routeTemplate: this.AreaName + "/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
}

I've created a post about how to implement the HttpControllerFactory to support Areas

And now i can just specify area name in MapHttpRoute in the Global.asax file:

routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { area = configurationService.SiteName, id = RouteParameter.Optional }
        );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!