要从ASP.NET MVC中的自定义帮助器方法中访问当前的System.Web.Routing.RequestContext,您可以使用以下方法:
例如:
public static string GetCurrentRouteData(this HtmlHelper helper)
{
var requestContext = HttpContext.Current.Request.RequestContext;
var routeData = requestContext.RouteData;
var controller = routeData.Values["controller"].ToString();
var action = routeData.Values["action"].ToString();
return $"Controller: {controller}, Action: {action}";
}
例如:
public static string GetRouteData(this HtmlHelper helper, RequestContext requestContext)
{
var routeData = requestContext.RouteData;
var controller = routeData.Values["controller"].ToString();
var action = routeData.Values["action"].ToString();
return $"Controller: {controller}, Action: {action}";
}
这样,您就可以在ASP.NET MVC中的自定义帮助器方法中访问当前的System.Web.Routing.RequestContext。
领取专属 10元无门槛券
手把手带您无忧上云