解讀ASP.NET 5 & MVC6系列教程(15):MvcOptions配置
程序模型處理 IApplicationModelConvention
在MvcOptions
的實(shí)例對(duì)象上,有一個(gè)ApplicationModelConventions
屬性(類型是:List<IApplicationModelConvention>
),該屬性IApplicationModelConvention
類型的接口集合,用于處理應(yīng)用模型ApplicationModel
,該集合是在MVC程序啟動(dòng)的時(shí)候進(jìn)行調(diào)用,所以在調(diào)用之前,我們可以對(duì)其進(jìn)行修改或更新,比如,我們可以針對(duì)所有的Controller和Action在數(shù)據(jù)庫(kù)中進(jìn)行授權(quán)定義,在程序啟動(dòng)的時(shí)候讀取數(shù)據(jù)授權(quán)信息,然后對(duì)應(yīng)用模型ApplicationModel
進(jìn)行處理。 示例如下:
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的實(shí)例對(duì)象中,有一個(gè)ViewEngines屬性用于保存系統(tǒng)的視圖引擎集合,以便可以讓我們實(shí)現(xiàn)自己的自定義視圖引擎,比如在《自定義View視圖文件查找邏輯》章節(jié)中,我們就利用了該特性,來(lái)實(shí)現(xiàn)了自己的自定義視圖引擎,示例如下:
services.AddMvc().Configure<MvcOptions>(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中,默認(rèn)的輸出格式化器有如下四種:
HttpNoContentOutputFormatter StringOutputFormatter JsonOutputFormatter XmlDataContractSerializerOutputFormatter
上述四種在系統(tǒng)中,是根據(jù)不同的情形自動(dòng)進(jìn)行判斷輸出的,具體判斷規(guī)則如下:
如果是如下類似的Action,則使用HttpNoContentOutputFormatter
返回204,即NoContent。
public Task DoSomethingAsync() { // 返回Task } public void DoSomething() { // Void方法 } public string GetString() { return null; // 返回null } public List<Data> GetData() { return null; // 返回null }
如果是如下方法,同樣是返回字符串,只有返回類型是string
的Action,才使用StringOutputFormatter
返回字符串;返回類型是object的Action,則使用JsonOutputFormatter
返回JSON類型的字符串?dāng)?shù)據(jù)。
public object GetData() { return"The Data"; // 返回JSON } public string GetString() { return"The Data"; // 返回字符串 }
如果上述兩種類型的Action都不是,則默認(rèn)使用JsonOutputFormatter
返回JSON數(shù)據(jù),如果JsonOutputFormatter
格式化器通過(guò)如下語(yǔ)句被刪除了,那就會(huì)使用XmlDataContractSerializerOutputFormatter
返回XML數(shù)據(jù)。
services.Configure<MvcOptions>(options => options.OutputFormatters.RemoveAll(formatter => formatter.Instance is JsonOutputFormatter) );
當(dāng)然,你也可以使用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 List<Data> GetMeData() { return GetDataFromSource(); } }
當(dāng)然,也可以在全局范圍內(nèi)聲明該ProducesAttribute
,示例如下:
services.Configure<MvcOptions>(options => options.Filters.Add(newProducesAttribute("application/json")) );
Output Cache 與 Profile
在MVC6中,OutputCache的特性由ResponseCacheAttribute
類來(lái)支持,示例如下:
[ResponseCache(Duration = 100)] public IActionResult Index() { return Content(DateTime.Now.ToString()); }
上述示例表示,將該頁(yè)面的內(nèi)容在客戶端緩存100秒,換句話說(shuō),就是在Response響應(yīng)頭header里添加一個(gè)Cache-Control
頭,并設(shè)置max-age=100
。 該特性支持的屬性列表如下:
屬性名稱 | 描述 |
---|---|
Duration | 緩存時(shí)間,單位:秒,示例: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
還支持一個(gè)CacheProfileNam
e屬性,以便可以讀取全局設(shè)置的profile信息配置,進(jìn)行緩存,示例如下:
[ResponseCache(CacheProfileName = "MyProfile")] public IActionResult Index() { return Content(DateTime.Now.ToString()); } public void ConfigureServices(IServiceCollection services) { services.Configure<MvcOptions>(options => { options.CacheProfiles.Add("MyProfile", new CacheProfile { Duration = 100 }); }); }
通過(guò)向MvcOptions
的CacheProfiles
屬性值添加一個(gè)名為MyProfile
的個(gè)性設(shè)置,可以在所有的Action上都使用該配置信息。
其它我們已經(jīng)很熟悉的內(nèi)容
以下內(nèi)容我們可能都已經(jīng)非常熟悉了,因?yàn)樵谥暗腗VC版本中都已經(jīng)使用過(guò)了,這些內(nèi)容均作為MvcOptions的屬性而存在,具體功能列表如下(就不一一敘述了):
FiltersModelBindersModelValidatorProvidersValidationExcludeFiltersValueProviderFactories
另外兩個(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,則不忽略。
相關(guān)文章
.NET 2.0獲取配置文件AppSettings和ConnectionStrings節(jié)數(shù)據(jù)的方法
.NET 2.0獲取配置文件AppSettings和ConnectionStrings節(jié)數(shù)據(jù)的方法...2007-12-12在ASP.NET 2.0中操作數(shù)據(jù)之四十五:DataList和Repeater里的自定義Button
本文主要介紹在DataList和Repeater添加諸如Button,LinkButton和ImageButton等控件的方法,并根據(jù)相關(guān)控件CommandName屬性觸發(fā)相應(yīng)的事件,執(zhí)行相應(yīng)的操作。2016-05-05在ASP.NET 2.0中操作數(shù)據(jù)之三十三:基于DataList和Repeater使用DropDownList過(guò)濾的主/
前面已經(jīng)介紹過(guò)使用DropDownList過(guò)濾的主/從報(bào)表,不過(guò)當(dāng)時(shí)是基于GridView,本文算是復(fù)習(xí)一下,基于DataList和Repeater再次實(shí)現(xiàn)一下相同的功能。2016-05-05使用.Net6中的WebApplication打造最小API
本文詳細(xì)講解了使用.Net6中的WebApplication打造最小API,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12決定何時(shí)使用 DataGrid、DataList 或 Repeater(ASP.NET 技術(shù)文章)
決定何時(shí)使用 DataGrid、DataList 或 Repeater(ASP.NET 技術(shù)文章)...2006-10-10讓Asp.NET的DataGrid可排序、可選擇、可分頁(yè)
讓Asp.NET的DataGrid可排序、可選擇、可分頁(yè)...2006-10-10解讀ASP.NET 5 & MVC6系列教程(15):MvcOptions配置
這篇文章主要介紹了ASP.NET 5 MVC6中MvcOptions配置方法,需要的朋友可以參考下2016-06-06在ASP.NET 2.0中操作數(shù)據(jù)之六十三:GridView實(shí)現(xiàn)批量刪除數(shù)據(jù)
本文主要介紹在GridView控件中包含一個(gè)checkbox列來(lái)實(shí)現(xiàn)復(fù)選多條數(shù)據(jù),在批量刪除按鈕的事件中通過(guò)for循環(huán)來(lái)一一刪除。2016-05-05ASP.NET中URL Routing和IIS上URL Rewriting的區(qū)別
這篇文章主要介紹了ASP.NET中URL Routing和IIS上URL Rewriting的區(qū)別,需要的朋友可以參考下。2016-06-06在ASP.NET 2.0中操作數(shù)據(jù)之六十四:GridView批量添加數(shù)據(jù)
前面介紹了批量更新,批量刪除數(shù)據(jù),這篇文章主要介紹如何實(shí)現(xiàn)批量添加數(shù)據(jù),當(dāng)然為了保證數(shù)據(jù)的完整性,我們?cè)谧鲞@些批量操作的時(shí)候,都使用了事務(wù)來(lái)實(shí)現(xiàn)。2016-05-05