zoukankan      html  css  js  c++  java
  • MVC命名空间中的~UrlHelper中的Action方法告诉我们方法重载的重要性(路由的统一)

    UrlHelper对象实际上对一个表示请求上下文的RequestContext和路由对象集合的RouteCollection对象的封装,它对外界是只读的,在建立UrlHelper对象时,为它赋值,它的原型是这样:

         public class UrlHelper
            {
    
         public UrlHelper(RequestContext requestContext);
            public UrlHelper(RequestContext requestContext, RouteCollection routeCollection);
    
            public RequestContext RequestContext { get; }
            public RouteCollection RouteCollection { get; }
           }
    而今天主要说一个它的Action方法的实现,实现MVC模式的路由地址功能,当你调整MVC路由时,它可以自动匹配,比用传统写死的链接要灵活的多,要注意的是,如果用这种方法进行URL传递时,如果信息是被URL编码的,那么在controller的action里接收时,也应该进行URL解码,但如果是写死的链接,那就不需要解码。
    以下是Action()方法的原型,供大家参考:
      public string Action(string actionName)
        {
            return this.Action(actionName, null, null, null, null);
        }
        public string Action(string actionName, object routeValues)
        {
            return this.Action(actionName, null, new RouteValueDictionary(routeValues), null, null);
        }
        public string Action(string actionName, string controllerName)
        {
            return this.Action(actionName, controllerName, null, null, null);
        }
        public string Action(string actionName, RouteValueDictionary routeValues)
        {
            return this.Action(actionName, null, routeValues, null, null);
        }
        public string Action(string actionName, string controllerName, object routeValues)
        {
            return this.Action(actionName, controllerName, new RouteValueDictionary(routeValues), null, null);
        }
        public string Action(string actionName, string controllerName, RouteValueDictionary routeValues)
        {
            return this.Action(actionName, controllerName, routeValues, null, null);
        }
        public string Action(string actionName, string controllerName, object routeValues, string protocol)
        {
            return this.Action(actionName, controllerName, new RouteValueDictionary(routeValues), protocol, null);
        }
        public string Action(string actionName, string controllerName, RouteValueDictionary routeValues, string protocol, string hostName)
        {
            controllerName = controllerName ?? (string)this.RequestContext.RouteData.Values["controller"];
            routeValues = routeValues ?? new RouteValueDictionary();
            routeValues.Add("action", actionName);
            routeValues.Add("controller", controllerName);
            string virtualPath = this.RouteCollection.GetVirtualPath(this.RequestContext, routeValues).VirtualPath;
            if (string.IsNullOrEmpty(protocol) && string.IsNullOrEmpty(hostName))
            {
                return virtualPath.ToLower();
            }
    
            protocol = protocol??"http";
            Uri uri = this.RequestContext.HttpContext.Request.Url;
            hostName = hostName ?? uri.Host + ":" + uri.Port;            
            return string.Format("{0}://{1}{2}", protocol, hostName, virtualPath).ToLower();
        }
    上在的代码是咱们的蒋老大总结出来的,在这里感谢他一下。事实上Action方法有多个重载,这在开发人员调用上容易了不少,大家只调用自己需要的方法即可,不用管没有参数是否要用null代替,这一点,微软告诉我们,做底层架构级开发的话,要为上级开发人员提供更加友好的,安全的代码展现
  • 相关阅读:
    WIN10下Java环境变量配置
    Oracle中用户的创建和权限设置
    Oracle表空间的创建与删除
    设置RHEL-7.0的运行级别
    发布项目到Tomcat(域名直接访问)
    centos7 打开80端口(网络搜集)
    MySql字符乱码问题解决(真)
    centos7安装workbench
    centos7添加Windows引导
    centos7下yum安装mysql(转)
  • 原文地址:https://www.cnblogs.com/lori/p/2423798.html
Copyright © 2011-2022 走看看