解读ASP.NET 5 & MVC6系列(15):MvcOptions配置

移动开发 Android
在MvcOptions的实例对象上,有一个ApplicationModelConventions属性(类型是:List),该属性IApplicationModelConvention类型的接口集合,用于处理应用模型ApplicationModel

 程序模型处理 IApplicationModelConvention

在MvcOptions的实例对象上,有一个ApplicationModelConventions属性(类型是:List),该属性IApplicationModelConvention类型的接口集合,用于处理应用模型ApplicationModel,该集合是在MVC程序启动的时候进行调用,所以在调用之前,我们可以对其进行修改或更新,比如,我们可以针对所有的Controller和Action在数据库中进行授权定义,在程序启动的时候读取数据授权信息,然后对应用模型ApplicationModel进行处理。 示例如下:

  1. public class PermissionCheckApplicationModelConvention : IApplicationModelConvention 
  2.     public void Apply(ApplicationModel application) 
  3.     { 
  4.         foreach (var controllerModel in application.Controllers) 
  5.         { 
  6.             var controllerType = controllerModel.ControllerType; 
  7.             var controllerName = controllerModel.ControllerName; 
  8.  
  9.             controllerModel.Actions.ToList().ForEach(actionModel => 
  10.             { 
  11.                 var actionName = actionModel.ActionName; 
  12.                 var parameters = actionModel.Parameters; 
  13.  
  14.                 // 根据判断条件,操作修改actionModel 
  15.             }); 
  16.  
  17.             // 根据判断条件,操作修改ControllerModel 
  18.         } 
  19.     } 
  20. }

视图引擎的管理ViewEngines

在MvcOptions的实例对象中,有一个ViewEngines属性用于保存系统的视图引擎集合,以便可以让我们实现自己的自定义视图引擎,比如在《自定义View视图文件查找逻辑》章节中,我们就利用了该特性,来实现了自己的自定义视图引擎,示例如下:

  1. services.AddMvc().Configure(options => {     options.ViewEngines.Clear();     options.ViewEngines.Add(typeof(ThemeViewEngine)); }); 

Web API中的输入(InputFormater)/输出(OutputFormater)

输入

Web API和目前的MVC的输入参数的处理,目前支持JSON和XML格式,具体的处理类分别如下:

  1. JsonInputFormatter 
  2. XmlDataContractSerializerInputFormatter 

输出

在Web API中,默认的输出格式化器有如下四种:

  1. HttpNoContentOutputFormatter 
  2. StringOutputFormatter 
  3. JsonOutputFormatter 
  4. XmlDataContractSerializerOutputFormatter 

上述四种在系统中,是根据不同的情形自动进行判断输出的,具体判断规则如下:

如果是如下类似的Action,则使用HttpNoContentOutputFormatter返回204,即NoContent。

  1. public Task DoSomethingAsync() 
  2.     // 返回Task 
  3.  
  4. public void DoSomething() 
  5.     // Void方法 
  6.  
  7. public string GetString() 
  8.     return null// 返回null 
  9.  
  10. public List GetData() {     return null// 返回null }

如果是如下方法,同样是返回字符串,只有返回类型是string的Action,才使用StringOutputFormatter返回字符串;返回类型是object的Action,则使用JsonOutputFormatter返回JSON类型的字符串数据

  1. public object GetData() 
  2.     return"The Data";  // 返回JSON 
  3.  
  4. public string GetString() 
  5.     return"The Data";  // 返回字符串 

如果上述两种类型的Action都不是,则默认使用JsonOutputFormatter返回JSON数据,如果JsonOutputFormatter格式化器通过如下语句被删除了,那就会使用XmlDataContractSerializerOutputFormatter返回XML数据。

  1. services.Configure(options =>     options.OutputFormatters.RemoveAll(formatter => formatter.Instance is JsonOutputFormatter) )

 

当然,你也可以使用ProducesAttribute显示声明使用JsonOutputFormatter格式化器,示例如下。

  1. public class Product2Controller : Controller 
  2.     [Produces("application/json")] 
  3.     //[Produces("application/xml")] 
  4.     public Product Detail(int id) 
  5.     { 
  6.         return new Product() { ProductId = id, ProductName = "商品名称" }; 
  7.     } 
  8. }

或者,可以在基类Controller上,也可以使用ProducesAttribute,示例如下:

  1. [Produces("application/json")] 
  2.     public class JsonController : Controller { } 
  3.  
  4.     public class HomeController : JsonController 
  5.     { 
  6.         public List GetMeData()         {             return GetDataFromSource();         }     }

当然,也可以在全局范围内声明该ProducesAttribute,示例如下:

  1. services.Configure(options =>         options.Filters.Add(newProducesAttribute("application/json"))     );

Output Cache 与 Profile

在MVC6中,OutputCache的特性由ResponseCacheAttribute类来支持,示例如下:

  1. [ResponseCache(Duration = 100)] 
  2. public IActionResult Index() 
  3.     return Content(DateTime.Now.ToString()); 
  4. }

上述示例表示,将该页面的内容在客户端缓存100秒,换句话说,就是在Response响应头header里添加一个Cache-Control头,并设置max-age=100。 该特性支持的属性列表如下:

解读ASP.NET 5 & MVC6系列(15):MvcOptions配置

另外,ResponseCacheAttribute还支持一个CacheProfileName属性,以便可以读取全局设置的profile信息配置,进行缓存,示例如下:

 

  1. [ResponseCache(CacheProfileName = "MyProfile")] 
  2. public IActionResult Index() 
  3.     return Content(DateTime.Now.ToString()); 
  4.  
  5. public void ConfigureServices(IServiceCollection services) 
  6.     services.Configure(options =>     {         options.CacheProfiles.Add("MyProfile",             new CacheProfile             {                 Duration = 100             });     }); } 
  7.  
  8.   

通过向MvcOptions的CacheProfiles属性值添加一个名为MyProfile的个性设置,可以在所有的Action上都使用该配置信息。

其它我们已经很熟悉的内容

以下内容我们可能都已经非常熟悉了,因为在之前的MVC版本中都已经使用过了,这些内容均作为MvcOptions的属性而存在,具体功能列表如下(就不一一叙述了):

1.Filters

2.ModelBinders

3.ModelValidatorProviders

4.ValidationExcludeFilters

5.ValueProviderFactories

另外两个:

MaxModelValidationErrors

置模型验证是显示的***错误数量。

RespectBrowserAcceptHeader

在使用Web API的内容协定功能时,是否遵守Accept Header的定义,默认情况下当media type默认是*/*的时候是忽略Accept header的。如果设置为true,则不忽略。

责任编辑:chenqingxiang 来源: 汤姆大叔的博客
相关推荐

2015-06-18 16:29:14

ASP.NET

2015-06-17 14:42:04

ASP.NET

2015-06-29 10:00:02

ASP.NETMVC6

2015-06-18 16:39:17

ASP.NET

2015-06-18 14:13:36

ASP.NET

2015-06-17 16:45:28

ASP.NET

2015-06-16 15:01:59

ASP.NET 5

2015-06-17 16:01:30

ASP.NET

2015-06-17 17:01:48

ASP.NET

2015-06-17 16:04:36

ASP.NET

2015-06-17 14:09:36

ASP.NET

2015-06-17 10:16:30

ASP.NET

2015-06-17 14:16:50

ASP.NET

2015-06-18 17:37:19

ASP.NET

2015-06-17 14:18:50

ASP.NET

2015-06-18 17:34:25

ASP.NET

2015-06-18 14:25:56

ASP.NET

2014-08-26 09:22:40

ASP.NET MVCRouting

2015-06-30 14:58:20

技术周刊

2009-07-31 12:43:59

ASP.NET MVC
点赞
收藏

51CTO技术栈公众号