佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 解讀ASP.NET 5 & MVC6系列教程(15):MvcOptions配置

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

熱門標簽:智能手機 服務(wù)器配置 解決方案 蘋果 電子圍欄 地方門戶網(wǎng)站 硅谷的囚徒呼叫中心 呼叫中心

程序模型處理 IApplicationModelConvention

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

public class PermissionCheckApplicationModelConvention : IApplicationModelConvention
{
 public void Apply(ApplicationModel application)
 {
  foreach (var controllerModel in application.Controllers)
  {
   var controllerType = controllerModel.ControllerType;
   var controllerName = controllerModel.ControllerName;

   controllerModel.Actions.ToList().ForEach(actionModel =>
   {
    var actionName = actionModel.ActionName;
    var parameters = actionModel.Parameters;

    // 根據(jù)判斷條件,操作修改actionModel
   });

   // 根據(jù)判斷條件,操作修改ControllerModel
  }
 }
}

視圖引擎的管理ViewEngines

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

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

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

輸入

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

JsonInputFormatter
XmlDataContractSerializerInputFormatter

輸出

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

HttpNoContentOutputFormatter
StringOutputFormatter
JsonOutputFormatter
XmlDataContractSerializerOutputFormatter

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

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

public Task DoSomethingAsync()
{
 // 返回Task
}

public void DoSomething()
{
 // Void方法
}

public string GetString()
{
 return null; // 返回null
}

public ListData> GetData()
{
 return null; // 返回null
}

如果是如下方法,同樣是返回字符串,只有返回類型是string的Action,才使用StringOutputFormatter返回字符串;返回類型是object的Action,則使用JsonOutputFormatter返回JSON類型的字符串數(shù)據(jù)。

public object GetData()
{
 return"The Data"; // 返回JSON
}

public string GetString()
{
 return"The Data"; // 返回字符串
}

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

services.ConfigureMvcOptions>(options =>
 options.OutputFormatters.RemoveAll(formatter => formatter.Instance is JsonOutputFormatter)
);

當然,你也可以使用ProducesAttribute顯示聲明使用JsonOutputFormatter格式化器,示例如下。

public class Product2Controller : Controller
{
 [Produces("application/json")]
 //[Produces("application/xml")]
 public Product Detail(int id)
 {
  return new Product() { ProductId = id, ProductName = "商品名稱" };
 }
}

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

[Produces("application/json")]
public class JsonController : Controller { }

public class HomeController : JsonController
{
 public ListData> GetMeData()
 {
  return GetDataFromSource();
 }
}

當然,也可以在全局范圍內(nèi)聲明該ProducesAttribute,示例如下:

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

Output Cache 與 Profile

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

[ResponseCache(Duration = 100)]
public IActionResult Index()
{
 return Content(DateTime.Now.ToString());
}

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

屬性名稱 描述
Duration 緩存時間,單位:秒,示例:Cache-Control:max-age=100
NoStore true則設(shè)置Cache-Control:no-store
VaryByHeader 設(shè)置Vary header頭
Location 緩存位置,如將Cache-Control設(shè)置為public, private或no-cache。

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

[ResponseCache(CacheProfileName = "MyProfile")]
public IActionResult Index()
{
 return Content(DateTime.Now.ToString());
}

public void ConfigureServices(IServiceCollection services)
{
 services.ConfigureMvcOptions>(options =>
 {
  options.CacheProfiles.Add("MyProfile",
   new CacheProfile
   {
    Duration = 100
   });
 });
}

通過向MvcOptionsCacheProfiles屬性值添加一個名為MyProfile的個性設(shè)置,可以在所有的Action上都使用該配置信息。

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

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

FiltersModelBindersModelValidatorProvidersValidationExcludeFiltersValueProviderFactories

另外兩個:
MaxModelValidationErrors
置模型驗證是顯示的最大錯誤數(shù)量。

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

您可能感興趣的文章:
  • 如何使用.NET Core 選項模式【Options】

標簽:喀什 ???/a> 呂梁 佳木斯 德宏 玉林 房產(chǎn) 泰安

巨人網(wǎng)絡(luò)通訊聲明:本文標題《解讀ASP.NET 5 & MVC6系列教程(15):MvcOptions配置》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    宽城| 峨山| 体育| 泸水县| 阆中市| 汾阳市| 昌图县| 资源县| 玉屏| 泸溪县| 安庆市| 临夏县| 昌黎县| 长阳| 老河口市| 阳原县| 泸州市| 海阳市| 城市| 仙居县| 漳州市| 博白县| 通州区| 北安市| 扬州市| 仙居县| 德安县| 宜兴市| 鹤岗市| 枝江市| 双峰县| 饶河县| 德钦县| 高阳县| 南宁市| 休宁县| 固安县| 谢通门县| 宝清县| 临海市| 泸定县|