How to get the Current Controller Name, Action, or ID in ASP.NET MVC
public static class HtmlRequestHelper { public static string Id(this HtmlHelper htmlHelper) { var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values; if (routeValues.ContainsKey("id")) return (string)routeValues["id"]; else if (HttpContext.Current.Request.QueryString.AllKeys.Contains("id")) return HttpContext.Current.Request.QueryString["id"]; return string.Empty; } public static string Controller(this HtmlHelper htmlHelper) { var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values; if (routeValues.ContainsKey("controller")) return (string)routeValues["controller"]; return string.Empty; } public static string Action(this HtmlHelper htmlHelper) { var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values; if (routeValues.ContainsKey("action")) return (string)routeValues["action"]; return string.Empty; } }
🐳 作者:dupeng0811 📢 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接 💬 留言:同时 , 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |
|