mvc 路由 .html 404,vs2017 mvc 自定义路由规则 出现 404.0 错误代码 0x80070002

///

///命名空间选择器 增加参数///

public classNamespaceHttpControllerSelector : IHttpControllerSelector

{private const string NamespaceKey = "namespace";private const string ControllerKey = "controller";private const string defaultNamespaceKey = "defaultnamespace";private readonlyHttpConfiguration _configuration;private readonly Lazy>_controllers;private readonly HashSet_duplicates;publicNamespaceHttpControllerSelector(HttpConfiguration config)

{

_configuration=config;

_duplicates= new HashSet(StringComparer.OrdinalIgnoreCase);

_controllers= new Lazy>(InitializeControllerDictionary);

}private DictionaryInitializeControllerDictionary()

{var dictionary = new Dictionary(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 controllerTypes =controllersResolver.GetControllerTypes(assembliesResolver);foreach (Type t incontrollerTypes)

{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]= newHttpControllerDescriptor(_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);

}returndictionary;

}//Get a value from the route data, if present.

private static T GetRouteVariable(IHttpRouteData routeData, stringname)

{object result = null;if (routeData.Values.TryGetValue(name, outresult))

{return(T)result;

}return default(T);

}publicHttpControllerDescriptor SelectController(HttpRequestMessage request)

{

IHttpRouteData routeData=request.GetRouteData();if (routeData == null)

{throw newHttpResponseException(HttpStatusCode.NotFound);

}//Get the namespace and controller variables from the route data.

string namespaceName = GetRouteVariable(routeData, NamespaceKey);if (namespaceName == null)

{if (routeData.Route.Defaults != null &&routeData.Route.Defaults.Keys.Contains(defaultNamespaceKey))

{

namespaceName=routeData.Route.Defaults[defaultNamespaceKey].ToString();

}else{throw newHttpResponseException(HttpStatusCode.NotFound);

}

}string controllerName = GetRouteVariable(routeData, ControllerKey);if (controllerName == null)

{throw newHttpResponseException(HttpStatusCode.NotFound);

}//Find a matching controller.

string key = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", namespaceName, controllerName);

HttpControllerDescriptor controllerDescriptor;if (_controllers.Value.TryGetValue(key, outcontrollerDescriptor))

{returncontrollerDescriptor;

}else if(_duplicates.Contains(key))

{throw newHttpResponseException(

request.CreateErrorResponse(HttpStatusCode.InternalServerError,"Multiple controllers were found that match this request."));

}else{throw newHttpResponseException(HttpStatusCode.NotFound);

}

}public IDictionaryGetControllerMapping()

{return_controllers.Value;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值