日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
解讀ASP.NET5&MVC6系列(15):MvcOptions配置

 程序模型處理 IApplicationModelConvention

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供新疆網(wǎng)站建設(shè)、新疆做網(wǎng)站、新疆網(wǎng)站設(shè)計(jì)、新疆網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、新疆企業(yè)網(wǎng)站模板建站服務(wù),十余年新疆做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

在MvcOptions的實(shí)例對(duì)象上,有一個(gè)ApplicationModelConventions屬性(類型是:List ),該屬性IApplicationModelConvention類型的接口集合,用于處理應(yīng)用模型ApplicationModel,該集合是在MVC程序啟動(dòng)的時(shí)候進(jìn)行調(diào)用,所以在調(diào)用之前,我們可以對(duì)其進(jìn)行修改或更新,比如,我們可以針對(duì)所有的Controller和Action在數(shù)據(jù)庫中進(jìn)行授權(quán)定義,在程序啟動(dòng)的時(shí)候讀取數(shù)據(jù)授權(quán)信息,然后對(duì)應(yīng)用模型ApplicationModel進(jìn)行處理。 示例如下:

 
 
 
 
  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.                 // 根據(jù)判斷條件,操作修改actionModel 
  15.             }); 
  16.  
  17.             // 根據(jù)判斷條件,操作修改ControllerModel 
  18.         } 
  19.     } 
  20. }

視圖引擎的管理ViewEngines

在MvcOptions的實(shí)例對(duì)象中,有一個(gè)ViewEngines屬性用于保存系統(tǒng)的視圖引擎集合,以便可以讓我們實(shí)現(xiàn)自己的自定義視圖引擎,比如在《自定義View視圖文件查找邏輯》章節(jié)中,我們就利用了該特性,來實(shí)現(xiàn)了自己的自定義視圖引擎,示例如下:

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

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

輸入

Web API和目前的MVC的輸入?yún)?shù)的處理,目前支持JSON和XML格式,具體的處理類分別如下:

 
 
 
 
  1. JsonInputFormatter 
  2. XmlDataContractSerializerInputFormatter 

輸出

在Web API中,默認(rèn)的輸出格式化器有如下四種:

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

上述四種在系統(tǒng)中,是根據(jù)不同的情形自動(dòng)進(jìn)行判斷輸出的,具體判斷規(guī)則如下:

如果是如下類似的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類型的字符串?dāng)?shù)據(jù)

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

如果上述兩種類型的Action都不是,則默認(rèn)使用JsonOutputFormatter返回JSON數(shù)據(jù),如果JsonOutputFormatter格式化器通過如下語句被刪除了,那就會(huì)使用XmlDataContractSerializerOutputFormatter返回XML數(shù)據(jù)。

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

當(dāng)然,你也可以使用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();         }     }

當(dāng)然,也可以在全局范圍內(nèi)聲明該P(yáng)roducesAttribute,示例如下:

 
 
 
 
  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. }

上述示例表示,將該頁面的內(nèi)容在客戶端緩存100秒,換句話說,就是在Response響應(yīng)頭header里添加一個(gè)Cache-Control頭,并設(shè)置max-age=100。 該特性支持的屬性列表如下:

另外,ResponseCacheAttribute還支持一個(gè)CacheProfileName屬性,以便可以讀取全局設(shè)置的profile信息配置,進(jìn)行緩存,示例如下:

 
 
 
 
  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屬性值添加一個(gè)名為MyProfile的個(gè)性設(shè)置,可以在所有的Action上都使用該配置信息。

其它我們已經(jīng)很熟悉的內(nèi)容

以下內(nèi)容我們可能都已經(jīng)非常熟悉了,因?yàn)樵谥暗腗VC版本中都已經(jīng)使用過了,這些內(nèi)容均作為MvcOptions的屬性而存在,具體功能列表如下(就不一一敘述了):

1.Filters

2.ModelBinders

3.ModelValidatorProviders

4.ValidationExcludeFilters

5.ValueProviderFactories

另外兩個(gè):

MaxModelValidationErrors

置模型驗(yàn)證是顯示的***錯(cuò)誤數(shù)量。

RespectBrowserAcceptHeader

在使用Web API的內(nèi)容協(xié)定功能時(shí),是否遵守Accept Header的定義,默認(rèn)情況下當(dāng)media type默認(rèn)是*/*的時(shí)候是忽略Accept header的。如果設(shè)置為true,則不忽略。


本文名稱:解讀ASP.NET5&MVC6系列(15):MvcOptions配置
路徑分享:http://www.dlmjj.cn/article/dpjohso.html