In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to configure MvcOptions, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
Program model processing IApplicationModelConvention
On the instance object of MvcOptions, there is an ApplicationModelConventions property (type: List), which is an interface collection of type IApplicationModelConvention, which is used to deal with the application model ApplicationModel. This collection is called when the MVC program starts, so we can modify or update it before calling it. For example, we can define authorization in the database for all Controller and Action, and read the data authorization information when the program starts. Then the application model ApplicationModel is processed. Examples are as follows:
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; / / modify actionModel} according to the judgment condition); / / modify ControllerModel}} according to the judgment condition
Management ViewEngines of View engine
In the instance object of MvcOptions, there is a ViewEngines property that holds the system's view engine collection so that we can implement our own custom view engine, as shown in the following example:
Services.AddMvc () .Configure (options = > {options.ViewEngines.Clear (); options.ViewEngines.Add (typeof (ThemeViewEngine));})
Input (InputFormater) / output (OutputFormater) in Web API
Input
The input parameters of Web API and MVC currently support JSON and XML formats. The specific processing classes are as follows:
JsonInputFormatterXmlDataContractSerializerInputFormatter
Output
In Web API, there are four default output formatters:
HttpNoContentOutputFormatterStringOutputFormatterJsonOutputFormatterXmlDataContractSerializerOutputFormatter
The above four kinds of output are automatically judged according to different situations in the system, and the specific judgment rules are as follows:
If it is an Action similar to the following, use HttpNoContentOutputFormatter to return 204, that is, NoContent.
Public Task DoSomethingAsync () {/ / return Task} public void DoSomething () {/ / Void method} public string GetString () {return null; / / return null} public List GetData () {return null; / / return null}
If the following method also returns a string, only Action with the return type string will use StringOutputFormatter to return the string; if the return type is Action of object, JsonOutputFormatter will be used to return string data of type JSON.
Public object GetData () {return "The Data"; / / return JSON} public string GetString () {return "The Data"; / / return string}
If the above two types of Action are not, JsonOutputFormatter is used by default to return JSON data, and if the JsonOutputFormatter formatter is deleted with the following statement, XmlDataContractSerializerOutputFormatter is used to return XML data.
Services.Configure (options = > options.OutputFormatters.RemoveAll (formatter = > formatter.Instance is JsonOutputFormatter))
Of course, you can also use the ProducesAttribute display declaration to use the JsonOutputFormatter formatter, as shown below.
Public class Product2Controller: Controller {[Produces ("application/json")] / / [Produces ("application/xml")] public Product Detail (int id) {return new Product () {ProductId = id, ProductName = "Trade name"};}}
Alternatively, you can use ProducesAttribute on the base class Controller, as shown in the following example:
[Produces ("application/json")] public class JsonController: Controller {} public class HomeController: JsonController {public List GetMeData () {return GetDataFromSource ();}
Of course, the ProducesAttribute can also be declared globally, as shown in the following example:
Services.Configure (options = > options.Filters.Add (newProducesAttribute ("application/json")
Output Cache and Profile
In MVC6, the OutputCache feature is supported by the ResponseCacheAttribute class, as shown in the following example:
[ResponseCache (Duration = 100)] public IActionResult Index () {return Content (DateTime.Now.ToString ());}
The above example shows that the contents of the page are cached on the client for 100 seconds, in other words, adding a Cache-Control header in the Response response header header and setting max-age=100. The list of attributes supported by this feature is as follows:
The attribute name describes the Duration cache time (in seconds). Example: Cache-Control:max-age=100NoStoretrue sets the Cache-Control:no-storeVaryByHeader to set the Vary header header Location cache location, such as setting Cache-Control to public, private or no-cache.
In addition, ResponseCacheAttribute also supports a CacheProfileName attribute so that the profile information configuration of global settings can be read and cached, as shown in the following example:
[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});});}
You can use this configuration information on all Action by adding a personality setting called MyProfile to the value of the CacheProfiles attribute of MvcOptions.
Other things that we are already familiar with.
We are probably already familiar with the following, because it has been used in previous versions of MVC and exists as attributes of MvcOptions. The specific features are listed below (not to be described one by one):
FiltersModelBindersModelValidatorProvidersValidationExcludeFiltersValueProviderFactories
The other two:
MaxModelValidationErrors
Set model validation is the maximum number of errors displayed.
RespectBrowserAcceptHeader
Whether or not to comply with the definition of Accept Header when using Web API's content agreement feature, Accept header is ignored by default when media type defaults to * / *. If set to true, it is not ignored.
The above is how to configure MvcOptions. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.