samedi 7 février 2015

Getting area, controller and action names inside filter attributes


Vote count:

0




I have global attribute in which i need to know the area, controller and action. I can't use RawUrl due to routes (inside area registration and attribute routing). I have followed the following two approaches but in both cases, my area is coming back as null. When i use a route then i get the area name just fine. Why is my area null when i do redirecttoaction or url.action or type the url manually etc? We are using MVC 5.0.



var routingValues = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current)).Values;

var currentArea = (string) routingValues["area"] ?? string.Empty;
var currentController = (string) routingValues["controller"] ?? string.Empty;
var currentAction = (string) routingValues["action"] ?? string.Empty;



var handler = HttpContext.Current.Handler as System.Web.Mvc.MvcHandler;
var area = handler.RequestContext.RouteData.Values["area"];
var controller = handler.RequestContext.RouteData.Values["controller"];
var action = handler.RequestContext.RouteData.Values["action"];


Like i said above, if i use a route (/TipHotLine) then i get the area name just fine.



public class AgencyAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Agency";
}
}

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Agency_default",
"Agency/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);

context.MapRoute(
"tiphotline",
"tiphotline",
new
{
controller = "tiphotline",
action = "Index",
Area = "Agency"
}
);


}
}


asked 1 min ago







Getting area, controller and action names inside filter attributes

Aucun commentaire:

Enregistrer un commentaire