<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      代碼改變世界

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

      2015-06-01 08:58  湯姆大叔  閱讀(8909)  評論(5)    收藏  舉報

      程序模型處理 IApplicationModelConvention

      MvcOptions的實例對象上,有一個ApplicationModelConventions屬性(類型是:List<IApplicationModelConvention>),該屬性IApplicationModelConvention類型的接口集合,用于處理應用模型ApplicationModel,該集合是在MVC程序啟動的時候進行調用,所以在調用之前,我們可以對其進行修改或更新,比如,我們可以針對所有的Controller和Action在數據庫中進行授權定義,在程序啟動的時候讀取數據授權信息,然后對應用模型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;
      
                      // 根據判斷條件,操作修改actionModel
                  });
      
                  // 根據判斷條件,操作修改ControllerModel
              }
          }
      }
      

      視圖引擎的管理ViewEngines

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

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

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

      輸入

      Web API和目前的MVC的輸入參數的處理,目前支持JSON和XML格式,具體的處理類分別如下:

      JsonInputFormatter
      XmlDataContractSerializerInputFormatter
      

      輸出

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

      HttpNoContentOutputFormatter
      StringOutputFormatter
      JsonOutputFormatter
      XmlDataContractSerializerOutputFormatter
      

      上述四種在系統中,是根據不同的情形自動進行判斷輸出的,具體判斷規則如下:

      如果是如下類似的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類型的字符串數據。

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

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

      services.Configure<MvcOptions>(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 List<Data> GetMeData()
              {
                  return GetDataFromSource();
              }
          }
      

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

          services.Configure<MvcOptions>(options =>
              options.Filters.Add(newProducesAttribute("application/json"))
          );
      

      Output Cache 與 Profile

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

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

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

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

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

      [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
                  });
          });
      }
      

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

      其它我們已經很熟悉的內容

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

      1. Filters
      2. ModelBinders
      3. ModelValidatorProviders
      4. ValidationExcludeFilters
      5. ValueProviderFactories

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

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

      同步與推薦

      本文已同步至目錄索引:解讀ASP.NET 5 & MVC6系列

      主站蜘蛛池模板: 爆乳日韩尤物无码一区| 亚洲中文字幕一区二区| 亚洲这里只有久热精品伊人| 青草青草久热精品视频在线观看| 方正县| 国产精品偷乱一区二区三区| 日本熟妇XXXX潮喷视频| 县级市| 久久一日本道色综合久久| 亚洲国产五月综合网| 久久精品一本到99热免费| 日韩深夜福利视频在线观看| 中文字幕亚洲无线码A| 花莲市| 日本中文字幕乱码免费| 日韩久久久久久中文人妻| 襄城县| 伊人色综合久久天天| 亚洲另类激情专区小说婷婷久| 精品少妇爆乳无码aⅴ区| 亚洲激情视频一区二区三区 | 亚洲自偷自偷在线成人网站传媒| 亚洲色欲在线播放一区| 九九热在线观看视频精品| 精品欧美h无遮挡在线看中文| 久久精品人人槡人妻人人玩av| 日本高清在线观看WWW色| 文山县| 婷婷综合缴情亚洲| 亚洲欧美一区二区成人片| 亚洲一区二区三区在线| 午夜免费啪视频| 国产一区韩国主播| 丰满无码人妻热妇无码区| 激情五月天一区二区三区| 性欧美三级在线观看| 夜夜躁日日躁狠狠久久av| 综合偷自拍亚洲乱中文字幕| 綦江县| 亚洲欧美日韩国产精品一区二区| 无码日韩精品一区二区三区免费|