我理解的路由做用有如下几个html
实话实说MVC的路由我不多去作扩展,在MVC4时代,还会去重写掉url的大小写,而在MVC5以后,MVC自带了配置去小写化url。不过有一个配置仍是必需要提一下那就是Area,在你的系统达到必定规模以后,Controllers经过Area来管理将会变得更容易。这里给出个人Area扩展,很简单可是很重要,注意子类必须以AreaRegistration结尾,一样遵循约定有限于配置的原则,固然你也能够重写。web
public abstract class AreaRegistrationBase : AreaRegistration { public override string AreaName { get { var item = GetType().Name; return item.Replace("AreaRegistration", ""); } } public override void RegisterArea(AreaRegistrationContext context) { context.MapLowerCaseUrlRoute( AreaName + "_default", AreaName.ToLower() + "/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); GlobalConfiguration.Configuration.Routes.MapHttpRoute( AreaName + "Api", "api/" + AreaName + "/{controller}/{action}/{id}", new { area = AreaName, id = RouteParameter.Optional, namespaceName = new[] { this.GetType().Namespace } } ); } }
上面MVC的路由也注册了Api的路由,固然还有更好的方法,因为WebApi,基本不须要Url.Action和HtmlAction,这种路由出栈的策略,不须要去经过路由生成Url,因此
咱们只须要作到路由的解析便可,而且webapi并无提供自带的area机制,因此我扩展了DefaultHttpControllerSelector,获取到路由中area和controller参数
而后完成拼接,而后反射类型查找,在最开始时候预先缓存了全部controller,因此性能还不错,因为原代码是反编译得到,因此linq部分有点糟糕,等我回公司拿到源代码再修改。json
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Web.Http; using System.Web.Http.Controllers; using System.Web.Http.Dispatcher; namespace Coralcode.Webapi.Route { public class AreaHttpControllerSelector : DefaultHttpControllerSelector { public static string CoralControllerSuffix = "ApiController"; private readonly HttpConfiguration _configuration; private readonly Lazy<ILookup<string, Type>> _apiControllerTypes; private ILookup<string, Type> ApiControllerTypes { get { return this._apiControllerTypes.Value; } } public AreaHttpControllerSelector(HttpConfiguration configuration) : base(configuration) { this._configuration = configuration; this._apiControllerTypes = new Lazy<ILookup<string, Type>>(new Func<ILookup<string, Type>>(this.GetApiControllerTypes)); } private ILookup<string, Type> GetApiControllerTypes() { return Enumerable.ToLookup<Type, string, Type>((IEnumerable<Type>) ServicesExtensions.GetHttpControllerTypeResolver(this._configuration.Services).GetControllerTypes(ServicesExtensions.GetAssembliesResolver(this._configuration.Services)), (Func<Type, string>) (t => t.Name.ToLower().Substring(0, t.Name.Length - AreaHttpControllerSelector.CoralControllerSuffix.Length)), (Func<Type, Type>) (t => t)); } public override HttpControllerDescriptor SelectController(HttpRequestMessage request) { string controllerName = this.GetControllerName(request); if (!string.IsNullOrWhiteSpace(controllerName)) { List<Type> list = Enumerable.ToList<Type>(this.ApiControllerTypes[controllerName.ToLower()]); if (Enumerable.Any<Type>((IEnumerable<Type>) list)) { IDictionary<string, object> values = HttpRequestMessageExtensions.GetRouteData(request).Values; string endString; if (values.Count > 1) { StringBuilder stringBuilder = new StringBuilder(); if (values.ContainsKey("area")) { stringBuilder.Append('.'); stringBuilder.Append(values["area"]); stringBuilder.Append('.'); stringBuilder.Append("controllers"); } if (values.ContainsKey("controller")) { stringBuilder.Append('.'); stringBuilder.Append(values["controller"]); stringBuilder.Append(AreaHttpControllerSelector.CoralControllerSuffix); } endString = stringBuilder.ToString(); } else endString = string.Format(".{0}{1}", (object) controllerName, (object) AreaHttpControllerSelector.CoralControllerSuffix); Type controllerType = Enumerable.FirstOrDefault<Type>((IEnumerable<Type>) Enumerable.OrderBy<Type, int>(Enumerable.Where<Type>((IEnumerable<Type>) list, (Func<Type, bool>) (t => t.FullName.EndsWith(endString, StringComparison.CurrentCultureIgnoreCase))), (Func<Type, int>) (t => Enumerable.Count<char>((IEnumerable<char>) t.FullName, (Func<char, bool>) (s => (int) s == 46))))); if (controllerType != (Type) null) return new HttpControllerDescriptor(this._configuration, controllerName, controllerType); } } return base.SelectController(request); } } }
Controller激活这部分是MVC和Ioc结合的核心,Ioc有三大部分api
MVC须要和Ioc激活首先咱们关注的就是哪里注册和哪里去实例化,因为MVC使用的是约定优先于配置的方式,全部的Controller都是以“Controller”结尾,因此这里我直接加载
程序集,而后找到全部类型以Controller结尾的去注册就行了,激活的话有如下三种方式,难易程度依次递增,嵌套深度也依次递增,这里我采用重写
默认DefaultControllerFactory的方式去激活,这样既能够用本身的也结合其余机制去实现,其余方式你们能够自行扩展。缓存
using Coralcode.Framework.Aspect.Unity; using Coralcode.Mvc.Resources; using System; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace Coralcode.Mvc.ControllerFactory { public class UnityControllerFactory : DefaultControllerFactory { public override IController CreateController(RequestContext requestContext, string controllerName) { return base.CreateController(requestContext, controllerName); } protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { if (controllerType == (Type) null) throw new HttpException(404, Messages.MvcBase_NotFoundPage); if (!UnityService.HasRegistered(controllerType)) return base.GetControllerInstance(requestContext, controllerType); return (IController) UnityService.Resolve(controllerType); } } } //webapi 激活以下 using Coralcode.Framework.Aspect.Unity; using System; using System.Net.Http; using System.Web.Http.Controllers; using System.Web.Http.Dispatcher; namespace Coralcode.Webapi.ControllerFactory { public class UnityControllerActivator : IHttpControllerActivator { public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) { IHttpController httpController = (IHttpController) UnityService.Resolve(controllerType); HttpRequestMessageExtensions.RegisterForDispose(request, httpController as IDisposable); return httpController; } } }
这里基本上都是Mvc的源代码,只是增长了一个日志功能而已,扒MVC源代码你们必定去尝试并发
using Coralcode.Framework.Log; using System; using System.Web; using System.Web.Mvc; namespace Coralcode.Mvc.Filters { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public class LogExceptionAttribute : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { if (filterContext == null) throw new ArgumentNullException("filterContext"); Exception exception = filterContext.Exception; LoggerFactory.Instance.Error(exception.ToString()); if (filterContext.IsChildAction || filterContext.ExceptionHandled || (!filterContext.HttpContext.IsCustomErrorEnabled || new HttpException((string) null, exception).GetHttpCode() != 500) || !this.ExceptionType.IsInstanceOfType((object) exception)) return; this.HandlerViewResultException(filterContext); } private void HandlerViewResultException(ExceptionContext filterContext) { string controllerName = (string) filterContext.RouteData.Values["controller"]; string actionName = (string) filterContext.RouteData.Values["action"]; HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); ExceptionContext exceptionContext = filterContext; ViewResult viewResult1 = new ViewResult(); viewResult1.ViewName = this.View; viewResult1.MasterName = this.Master; viewResult1.ViewData = (ViewDataDictionary) new ViewDataDictionary<HandleErrorInfo>(model); viewResult1.TempData = filterContext.Controller.TempData; ViewResult viewResult2 = viewResult1; exceptionContext.Result = (ActionResult) viewResult2; filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.Clear(); filterContext.HttpContext.Response.StatusCode = 500; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; } } }
这里统一处理了Ajax请求返回数据以ResultMessage返回,若是不是JsonResult选择忽略,具体设计初衷能够看Controller设计那一节.异步
using Coralcode.Framework.Models; using Coralcode.Mvc.ActionResults; using System.Web.Mvc; namespace Coralcode.Mvc.Filters { public class ResultMessageAttribute : ActionFilterAttribute { public override void OnResultExecuting(ResultExecutingContext filterContext) { JsonResult jsonResult = (JsonResult) (filterContext.Result as CustomJsonResult) ?? filterContext.Result as JsonResult; if (jsonResult == null) return; jsonResult.Data = this.GetResultMessage(jsonResult.Data); filterContext.Result = (ActionResult) jsonResult; } private object GetResultMessage(object data) { if (data is BaseMessage) return data; return (object) new ResultMessage(ResultState.Success, "Success", data); } } }
提升并发利器,最佳使用方式,这里详细的介绍能够结合Artec的Controller同步和异步这一节,其中我我的推荐的就是采用返回Task<ActionResult>。ide
这里会将Url中的参数做为Model绑定的数据源。在作三级菜单的时候能够体现出用途,例如我首先在数据字典中添加以下数据模块化
var newsType = new Glossary() { Key = "NewsType", Value = "NewsType", Description = "新闻", Seq = 0, Title = "新闻类型", ParentId = -1, }; _glossaryService.Add(newsType); _glossaryService.Add(new Glossary() { Key = "RecentNews", Value = "RecentNews", Description = "新闻类型", Seq = 1, Title = "最新活动", ParentId = newsType.Id, }); _glossaryService.Add(new Glossary() { Key = "UserActivity", Value = "UserActivity", Description = "新闻类型", Seq = 2, Title = "会员活动", ParentId = newsType.Id, }); _glossaryService.Add(new Glossary() { Key = "OnlineMarket", Value = "OnlineMarket", Description = "新闻类型", Seq = 3, Title = "在线商城", ParentId = newsType.Id, }); _glossaryService.Add(new Glossary() { Key = "AboutUs", Value = "AboutUs", Description = "新闻类型", Seq = 4, Title = "关于咱们", ParentId = newsType.Id, }); Repository.UnitOfWork.Commit();
而后从字段中读出数据去添加菜单高并发
var newsMenu = Regist("新闻管理", "/portal/home/handerindex?menuId=" + systemManagerMenu.Identify + "-NewsType", systemManagerMenu.Identify, systemManagerMenu.Identify + "-NewsType"); //新闻管理 _glossaryService.GetFiltered("新闻类型").ForEach(item => { Regist(item.Title,string.Format( "/portal/news/index?typeid={0}&type={1}" , item.Id,item.Title), newsMenu.Identify, newsMenu.Identify + "-" + item.Id); });
加载三级菜单
/// <summary> /// 处理界面 /// </summary> /// <returns></returns> public ActionResult HanderIndex(string menuId) { ViewBag.Tree = string.Empty; //TODO:请求两次,待处理 if (menuId == null) return View(); var items = _menuService.GetChildrenMenus(menuId); ViewBag.Tree = JsonConvert.SerializeObject(items); return View(); }
界面如图
而后在Search和ViewModel中有一个字段是TypeId,这样在List,PageSearch,AddOrEdit中就能够自动绑定值了。
public class NewsSearch:SearchBase { public long? TypeId { get; set; } public string Title { get; set; } }
MVC默认的系统比较弱,当controller和view比较多的时候一个文件下面内容会很是多,我这里作了一个模块化处理.
using System.Web.Mvc; namespace Coralcode.Mvc.ViewEngines { public class ThemesRazorViewEngine : RazorViewEngine { public ThemesRazorViewEngine() { this.AreaViewLocationFormats = new string[3] { "~/Themes/{2}/{1}/{0}.cshtml", "~/Themes/Shared/{0}.cshtml", "~/Themes/{2}/Shared/{0}.cshtml" }; this.AreaMasterLocationFormats = new string[1] { "~/Themes/Shared/{0}.cshtml" }; this.AreaPartialViewLocationFormats = new string[4] { "~/Themes/{2}/{1}/{0}.cshtml", "~/Themes/{2}/Shared/{0}.cshtml", "~/Themes/Shared/{0}.cshtml", "~/Themes/Shared/Control/{0}.cshtml" }; this.ViewLocationFormats = new string[2] { "~/Themes/{1}/{0}.cshtml", "~/Themes/Shared/{0}.cshtml" }; this.MasterLocationFormats = new string[1] { "~/Themes/Shared/{0}.cshtml" }; this.PartialViewLocationFormats = new string[2] { "~/Themes/{1}/{0}.cshtml", "~/Themes/Shared/{0}.cshtml" }; } public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { if (controllerContext.RouteData.Values.ContainsKey("area") && !controllerContext.RouteData.DataTokens.ContainsKey("area")) controllerContext.RouteData.DataTokens.Add("area", controllerContext.RouteData.Values["area"]); return base.FindView(controllerContext, viewName, masterName, useCache); } }
项目目录结构以下图
这样子发布后的目录很是的干净,如图:
这部分在Controller设计那一节有详细说明,请往前一步参考