Controllers split by areas [duplicate]
Asked Answered
P

3

7

Possible Duplicate:
How do I register a controller that has been created in an AREA

I have the question - is it possible to do the next?

I have three Areas:

_Default
SiteOne
SiteTwo

Inside each area i have a ApiController with the same name, but in different namespaces of course:

MvcAppliaction.Areas._Default.Controllers.ValuesController
MvcAppliaction.Areas.SiteOne.Controllers.ValuesController
MvcAppliaction.Areas.SiteTwo.Controllers.ValuesController

I also have a value of current (which i would like to use) Area in configuration.

I would like to map user to controller in the proper Area (which i can find in the configuration) if he enters in the browser:

/api/values

For example, if current Area in config file is SiteOne then this request should be mapped to MvcAppliaction.Areas.SiteOne.Controllers.ValuesController controller, but if i change current Area in config file to SiteTwo of _Default it should be mapped to correct controller.

PS. With MVC controller it's easy, you just have to set your route:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new[] { "MvcApplication.Web.Areas." + SiteName + ".Controllers") }
);
Periphrastic answered 23/3, 2012 at 8:29 Comment(0)
Z
4

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.

Zedoary answered 24/3, 2012 at 16:21 Comment(2)
Thanks. I thought that there is another way. So, i will implement HTTP controllers factory.Periphrastic
No problem glad to be of helpZedoary
M
12

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 }
        );
}
Modular answered 24/3, 2012 at 3:54 Comment(2)
in this case, to access the controllers you have to write /siteone/values or /sitetwo/values. But I want to write just /api/values and get the access to controller from Area which I configured in config file. For example, if I specified SiteOne in config file and called the url /api/values i will get the controller from SiteOne Area, and if I will change area in config file SiteTwo, then if I will call _/api/values/ i will get the SiteTwo controller.Periphrastic
I'd like to propose name: this.AreaName + "_API" or whatever naming convention suits you - so the route name will be Admin_API instead of just Admin. In addition I've used routeTemplate: "api/" + this.AreaName + "/{controller}/{id}" so I can access it with /api/admin/users/123. If you get 404 errors then you probably have a conflict with another normal MVC routeAllowance
P
7

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 }
        );
Periphrastic answered 26/3, 2012 at 14:50 Comment(0)
Z
4

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.

Zedoary answered 24/3, 2012 at 16:21 Comment(2)
Thanks. I thought that there is another way. So, i will implement HTTP controllers factory.Periphrastic
No problem glad to be of helpZedoary

© 2022 - 2024 — McMap. All rights reserved.