• vs2017 mvc 自定义路由规则 出现 404.0 错误代码 0x80070002


    自定义: WebApiConfig  里面最后增加

      config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config));

     自定义的规则 可以根据命名空间;  跟原本默认的规则会有冲突;

    https://www.cnblogs.com/tx720/p/7666356.html

    如果要自定义mvc的 webapi路由规则。 需要在配置文件 web.config 里面增加

    <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />

    </system.webServer>

    NamespaceHttpControllerSelector:代码

        /// <summary>
        /// 命名空间选择器  增加参数
        /// </summary>
        public class NamespaceHttpControllerSelector : IHttpControllerSelector
        {
            private const string NamespaceKey = "namespace";
            private const string ControllerKey = "controller";
            private const string defaultNamespaceKey = "defaultnamespace";
            private readonly HttpConfiguration _configuration;
            private readonly Lazy<Dictionary<string, HttpControllerDescriptor>> _controllers;
            private readonly HashSet<string> _duplicates;
    
    
            public NamespaceHttpControllerSelector(HttpConfiguration config)
            {
                _configuration = config;
                _duplicates = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
                _controllers = new Lazy<Dictionary<string, HttpControllerDescriptor>>(InitializeControllerDictionary);
            }
    
            private Dictionary<string, HttpControllerDescriptor> InitializeControllerDictionary()
            {
                var dictionary = new Dictionary<string, HttpControllerDescriptor>(StringComparer.OrdinalIgnoreCase);
    
                // Create a lookup table where key is "namespace.controller". The value of "namespace" is the last
                // segment of the full namespace. For example:
                // MyApplication.Controllers.V1.ProductsController => "V1.Products"
                IAssembliesResolver assembliesResolver = _configuration.Services.GetAssembliesResolver();
                IHttpControllerTypeResolver controllersResolver = _configuration.Services.GetHttpControllerTypeResolver();
    
                ICollection<Type> controllerTypes = controllersResolver.GetControllerTypes(assembliesResolver);
    
                foreach (Type t in controllerTypes)
                {
                    var segments = t.Namespace.Split(Type.Delimiter);
    
                    // For the dictionary key, strip "Controller" from the end of the type name.
                    // This matches the behavior of DefaultHttpControllerSelector.
                    var controllerName = t.Name.Remove(t.Name.Length - DefaultHttpControllerSelector.ControllerSuffix.Length);
    
                    var key = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", segments[segments.Length - 1], controllerName);
    
                    // Check for duplicate keys.
                    if (dictionary.Keys.Contains(key))
                    {
                        _duplicates.Add(key);
                    }
                    else
                    {
                        dictionary[key] = new HttpControllerDescriptor(_configuration, t.Name, t);
                    }
                }
    
                // Remove any duplicates from the dictionary, because these create ambiguous matches. 
                // For example, "Foo.V1.ProductsController" and "Bar.V1.ProductsController" both map to "v1.products".
                foreach (string s in _duplicates)
                {
                    dictionary.Remove(s);
                }
                return dictionary;
            }
    
            // Get a value from the route data, if present.
            private static T GetRouteVariable<T>(IHttpRouteData routeData, string name)
            {
                object result = null;
                if (routeData.Values.TryGetValue(name, out result))
                {
                    return (T)result;
                }
                return default(T);
            }
    
            public HttpControllerDescriptor SelectController(HttpRequestMessage request)
            {
                IHttpRouteData routeData = request.GetRouteData();
                if (routeData == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
    
                // Get the namespace and controller variables from the route data.
                string namespaceName = GetRouteVariable<string>(routeData, NamespaceKey);
                if (namespaceName == null)
                {
                    if (routeData.Route.Defaults != null && routeData.Route.Defaults.Keys.Contains(defaultNamespaceKey))
                    {
                        namespaceName = routeData.Route.Defaults[defaultNamespaceKey].ToString();
                    }
                    else
                    {
                        throw new HttpResponseException(HttpStatusCode.NotFound);
                    }
                }
    
                string controllerName = GetRouteVariable<string>(routeData, ControllerKey);
                if (controllerName == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
    
                // Find a matching controller.
                string key = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", namespaceName, controllerName);
    
                HttpControllerDescriptor controllerDescriptor;
                if (_controllers.Value.TryGetValue(key, out controllerDescriptor))
                {
                    return controllerDescriptor;
                }
                else if (_duplicates.Contains(key))
                {
                    throw new HttpResponseException(
                        request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                        "Multiple controllers were found that match this request."));
                }
                else
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
            }
    
            public IDictionary<string, HttpControllerDescriptor> GetControllerMapping()
            {
                return _controllers.Value;
            }
        }
    View Code
  • 相关阅读:
    汇编--基础分析:数据段在内存中的存放及空间
    汇编语言(王爽)第六章检测点与实验5
    C语言经典例题100(22~40)
    C:数组小结(3)
    随机不重复的取数组元素,并赋值给div使用
    关于Apple设备私有的apple-touch-icon属性详解
    测试你女友是否跟你结婚插件的源码
    CSS子元素设置margin-top作用于父容器?
    hybrid app开发中:苹果移动设备实用Meta标签
    chromium 修改chromium的设置选项
  • 原文地址:https://www.cnblogs.com/zxs-onestar/p/11556139.html
Copyright © 2020-2023  润新知