zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    

      

  • 相关阅读:
    调试与分析
    GCC
    汇编
    数据恢复
    TCP/IP
    shell
    vmstat、top
    计算程序运行时间的封装
    protobuf
    c++模板
  • 原文地址:https://www.cnblogs.com/dupeng0811/p/4126888.html
Copyright © 2011-2022 走看看