????????Filter
????MVC?????????????Filter??IAuthorizationFilter??IActionFilter??IResultFilter??IExceptionFilter
?????????????е?????????????????磺?????????????IExceptionFilter?????Filter???????Action??????????н?????IActionFilter?????Filter?????

??????????IExceptionFilter???????????????????????????Log4NetExceptionFilter??
????public class Log4NetExceptionFilter : IExceptionFilter
????{
????private readonly ILog _logger;
????public Log4NetExceptionFilter()
????{
????_logger = LogManager.GetLogger(GetType());
????}
????public void OnException(ExceptionContext context)
????{
????_logger.Error("Unhandled exception"?? context.Exception);
????}
????}
?????????????????Filter????MVC??Filter?б??У?
????public class FilterConfig
????{
????public static void RegisterGlobalFilters(GlobalFilterCollection filters)
????{
????filters.Add(new Log4NetExceptionFilter());
????}
????}
?????????Action?????????????????Action?????????Action??н???????log??
????public class StopwatchAttribute : ActionFilterAttribute
????{
????private const string StopwatchKey = "StopwatchFilter.Value";
????private readonly ILog _logger= LogManager.GetLogger(typeof(StopwatchAttribute));
????public override void OnActionExecuting(ActionExecutingContext filterContext)
????{
????filterContext.HttpContext.Items[StopwatchKey] = Stopwatch.StartNew();
????}
????public override void OnActionExecuted(ActionExecutedContext filterContext)
????{
????var stopwatch = (Stopwatch)filterContext.HttpContext.Items[StopwatchKey];
????stopwatch.Stop();
????var log=string.Format("controller:{0}??action:{1}??execution time:{2}ms"??filterContext.ActionDescriptor.ControllerDescriptor.ControllerName??filterContext.ActionDescriptor.ActionName??stopwatch.ElapsedMilliseconds)
????_logger.Info(log);
????}
????}
????ActionFilterAttribute??????????????????????IActionFilter?? IResultFilter??Filter?????????FilterAttribute?????????ζ??????????????????????????Attribute?????????Action????Controller??????????????Filter????????????MVC??Filter???????????????á?
????????HtmlHelper
??????Razor????У???????д??ι??????????html????????????????????@helper???????磺
????@helper ShowProduct(List products?? string style)
????{
????@foreach (var product in products)
????{
????@product.Name
????}
????}
????????δ????е?????????????壬???????????list????????????????????????html??
????Product list using helper
????@ShowProduct(Model.SportProducts?? "list-group-item-info")
????@ShowProduct(Model.BookProducts?? "list-group-item-warning")
????@ShowProduct(Model.FoodProducts?? "list-group-item-danger")
??????????????????????????Ч???????????????????湫????????????????
??????Razor??????@Html??????HtmlHelper???????????????????????@Html.TextBox(“name”)????????????????????????????HtmlHelper???
public static class HtmlHelperExtensions
{
public static ListGroup ListGroup(this HtmlHelper htmlHelper)
{
return new ListGroup();
}
}
public class ListGroup
{
public MvcHtmlString Info(List data?? Func getName)
{
return Show(data??getName?? "list-group-item-info");
}
public MvcHtmlString Warning(List data?? Func getName)
{
return Show(data??getName?? "list-group-item-warning");
}
public MvcHtmlString Danger(List data?? Func getName)
{
return Show(data??getName?? "list-group-item-danger");
}
public MvcHtmlString Show(List data?? Func getName?? string style)
{
var ulBuilder = new TagBuilder("ul");
ulBuilder.AddCssClass("list-group");
foreach (T item in data)
{
var liBuilder = new TagBuilder("li");
liBuilder.AddCssClass("list-group-item");
liBuilder.AddCssClass(style);
liBuilder.SetInnerText(getName(item));
ulBuilder.InnerHtml += liBuilder.ToString();
}
return new MvcHtmlString(ulBuilder.ToString());
}
}
????????????????????????????????
????Product list using htmlHelper
????@Html.ListGroup().Info(Model.SportProducts??x=>x.Name)
????@Html.ListGroup().Warning(Model.BookProducts??x => x.Name)
????@Html.ListGroup().Danger(Model.FoodProducts??x => x.Name)
????Ч????

???????RazorViewEngine
????????????RazorViewEngine??????????????????????????View??????????????????????Theme????л?????????????????????????????????л???????????????????????RazorViewEngine??????

??????????????????????Theme?л???????????????????ViewEngine??
public class ThemeViewEngine: RazorViewEngine
{
public ThemeViewEngine(string theme)
{
ViewLocationFormats = new[]
{
"~/Views/Themes/" + theme + "/{1}/{0}.cshtml"??
"~/Views/Themes/" + theme + "/Shared/{0}.cshtml"
};
PartialViewLocationFormats = new[]
{
"~/Views/Themes/" + theme + "/{1}/{0}.cshtml"??
"~/Views/Themes/" + theme + "/Shared/{0}.cshtml"
};
AreaViewLocationFormats = new[]
{
"~Areas/{2}/Views/Themes/" + theme + "/{1}/{0}.cshtml"??
"~Areas/{2}/Views/Themes/" + theme + "/Shared/{0}.cshtml"
};
AreaPartialViewLocationFormats = new[]
{
"~Areas/{2}/Views/Themes/" + theme + "/{1}/{0}.cshtml"??
"~Areas/{2}/Views/Themes/" + theme + "/Shared/{0}.cshtml"
};
}
}
?????????????????ViewEngine???Razor????/Views/Themes/??????????View??????????????????ViewEngine???????ThemeViewEngine????ViewEngines
????public class MvcApplication : System.Web.HttpApplication
????{
????protected void Application_Start()
????{
????if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["Theme"]))
????{
????var activeTheme = ConfigurationManager.AppSettings["Theme"];
????ViewEngines.Engines.Insert(0?? new ThemeViewEngine(activeTheme));
????};
????//...
????}
????}