這篇文章將為大家詳細(xì)講解有關(guān)ASP.NET中怎么配置MvcOptions,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
專注于為中小企業(yè)提供成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)江孜免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
程序模型處理 IApplicationModelConvention
在MvcOptions的實例對象上,有一個ApplicationModelConventions屬性(類型是:List
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().Configure(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ù)不同的情形自動進(jìn)行判斷輸出的,具體判斷規(guī)則如下:
如果是如下類似的Action,則使用HttpNoContentOutputFormatter返回204,即NoContent。
public Task DoSomethingAsync()
{
// 返回Task
}
public void DoSomething()
{
// Void方法
}
public string GetString()
{
return null; // 返回null
}
public List 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格式化器通過如下語句被刪除了,那就會使用XmlDataContractSerializerOutputFormatter返回XML數(shù)據(jù)。
services.Configure(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 GetMeData() { return GetDataFromSource(); } }
當(dāng)然,也可以在全局范圍內(nèi)聲明該P(yáng)roducesAttribute,示例如下:
services.Configure(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。 該特性支持的屬性列表如下:
另外,ResponseCacheAttribute還支持一個CacheProfileName屬性,以便可以讀取全局設(shè)置的profile信息配置,進(jìn)行緩存,示例如下:
[ResponseCache(CacheProfileName = "MyProfile")] public IActionResult Index() { return Content(DateTime.Now.ToString()); } public void ConfigureServices(IServiceCollection services) { services.Configure(options => { options.CacheProfiles.Add("MyProfile", new CacheProfile { Duration = 100 }); }); }
通過向MvcOptions的CacheProfiles屬性值添加一個名為MyProfile的個性設(shè)置,可以在所有的Action上都使用該配置信息。
關(guān)于ASP.NET中怎么配置MvcOptions就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享標(biāo)題:ASP.NET中怎么配置MvcOptions
URL地址:http://muchs.cn/article24/piddje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計公司、面包屑導(dǎo)航、品牌網(wǎng)站設(shè)計、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)