标签:out text object 开发 接口 namespace defaults ide throw
1 public override void RegisterArea(AreaRegistrationContext context) 2 { 3 context.MapRoute( 4 "YZC_default", 5 "YZC/{controller}/{action}/{id}", 6 new { action = "Index", id = UrlParameter.Optional }, 7 new string[] { "YZC.Controllers" }//添加域项目的控制层命名空间 8 ); 9 }
1 { 2 public class ChildrenController : ApiController 3 { 4 #region 跨项目路由测试 5 [HttpGet] 6 public string Test() 7 { 8 return "小良哥"; 9 } 10 #endregion 11 } 12 }
1 /// <summary> 2 /// 自定义区域类 3 /// </summary> 4 public class CustomHttpControllerSelector: DefaultHttpControllerSelector 5 { 6 private const string NamespaceRouteVariableName = namespaceName"; 7 //namespaceName之后要用到 8 //private const string AreaRouteVariableName = "area"; 9 private readonly HttpConfiguration _configuration; 10 private readonly Lazy<ConcurrentDictionary<string, Type>> _apiControllerCache; 11 12 public CustomHttpControllerSelector(HttpConfiguration configuration) 13 : base(configuration) 14 { 15 _configuration = configuration; 16 17 _apiControllerCache = new Lazy<ConcurrentDictionary<string, Type>>( 18 new Func<ConcurrentDictionary<string, Type>>(InitializeApiControllerCache)); 19 } 20 21 22 private ConcurrentDictionary<string, Type> InitializeApiControllerCache() 23 { 24 IAssembliesResolver assembliesResolver = this._configuration.Services.GetAssembliesResolver(); 25 var types = this._configuration.Services.GetHttpControllerTypeResolver() 26 .GetControllerTypes(assembliesResolver).ToDictionary(t => t.FullName, t => t); 27 28 return new ConcurrentDictionary<string, Type>(types); 29 } 30 31 public IEnumerable<string> GetControllerFullName(HttpRequestMessage request, string controllerName) 32 { 33 object namespaceName; 34 var data = request.GetRouteData(); 35 IEnumerable<string> keys = _apiControllerCache.Value.ToDictionary<KeyValuePair<string, Type>, string, Type>(t => t.Key, 36 t => t.Value, StringComparer.OrdinalIgnoreCase).Keys.ToList(); 37 38 if (!data.Values.TryGetValue(NamespaceRouteVariableName, out namespaceName)) 39 { 40 return from k in keys 41 where k.EndsWith(string.Format(".{0}{1}", controllerName, 42 DefaultHttpControllerSelector.ControllerSuffix), StringComparison.OrdinalIgnoreCase) 43 select k; 44 } 45 46 string[] namespaces = (string[])namespaceName; 47 return from n in namespaces 48 join k in keys on string.Format("{0}.{1}{2}", n, controllerName, 49 DefaultHttpControllerSelector.ControllerSuffix).ToLower() equals k.ToLower() 50 select k; 51 } 52 53 54 public override HttpControllerDescriptor SelectController(HttpRequestMessage request) 55 { 56 Type type; 57 if (request == null) 58 { 59 throw new ArgumentNullException("request"); 60 } 61 62 string controllerName = this.GetControllerName(request); 63 if (string.IsNullOrEmpty(controllerName)) 64 { 65 throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound, 66 string.Format("No route providing a controller name was found to match request URI ‘{0}‘", 67 new object[] { request.RequestUri }))); 68 } 69 70 IEnumerable<string> fullNames = GetControllerFullName(request, controllerName); 71 if (fullNames.Count() == 0) 72 { 73 throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound, 74 string.Format("No route providing a controller name was found to match request URI ‘{0}‘", 75 new object[] { request.RequestUri }))); 76 } 77 78 if (!this._apiControllerCache.Value.TryGetValue(fullNames.First(), out type)) 79 { 80 throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound, 81 string.Format("No route providing a controller name was found to match request URI ‘{0}‘", 82 new object[] { request.RequestUri }))); 83 } 84 85 return new HttpControllerDescriptor(_configuration, controllerName, type); 86 } 87 }
1 namespace Momoda.Api 2 { 3 public class YZCAreaRegistration : AreaRegistration 4 { 5 public override string AreaName//域名称 6 { 7 get 8 { 9 return "YZC"; 10 } 11 } 12 public override void RegisterArea(AreaRegistrationContext context) 13 { 14 context.Routes.MapHttpRoute( 15 name: this.AreaName + "_default", 16 routeTemplate: "api/" + this.AreaName + "/{controller}/{action}/{id}", 17 defaults: new 18 { 19 id = RouteParameter.Optional, 20 namespaceName = new string[] { string.Format("{0}.Controllers",this.AreaName) } 21 //namespaceName名称要CustomHttpControllerSelector中NamespaceRouteVariableName的值一致 22 } 23 ); 24 } 25 } 26 }
标签:out text object 开发 接口 namespace defaults ide throw
原文地址:http://www.cnblogs.com/xiaoliangge/p/6027617.html