Reputation: 1220
Is there a way to check whether the Controller which invoked a method comes from a Controller which is within an Area?
For example, I have a class which inherits from AuthorizeAttribute e.g.
public class CustomAuthorize: System.Web.Mvc.AuthorizeAttribute
{
public CustomAuthorize()
{
...
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
// TODO - Check if the controller is from an Area
}
}
I then have some controller actions which are decorated with relevant Roles (as well as a few other custom attributes) e.g.
[CustomAuthorize(Roles ="Administrator")]
[HttpGet]
public virtual ActionResult Index()
{
...
}
In the TODO section above, I'd like to see if the Controller is one of the controllers from one of my Areas. I know that my controllers which are in an area will be in the ProjectName.Areas.xxx.Controllers namespace (where xxx is the Area name), whereas ones which aren't will be in the ProjectName.Controllers namespace.
Is there some way (using reflection perhaps?) that from within the AuthorizeCore function above that I can work out the specific area (or namespace that it came from) so that I can implement some custom functionality?
Upvotes: 3
Views: 72
Reputation: 146
I'm not too familiar with MCV's Area concept, but I've found this link from a Google search. Perhaps it may help you out.
ASP.NET MVC - Get Current Area Name in View or Controller
Upvotes: 0
Reputation: 12815
You can get it from the RouteData.DataTokens
:
httpContext.Request.RequestContext.RouteData.DataTokens["area"]
That will return null
if your controller is not in an area or the name of the area if your controller is in an area.
Upvotes: 2