麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

ASP.NET Web API Model-ParameterBinding

2019-11-17 01:46:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

asp.net Web API Model-ParameterBinding

ASP.NET Web API Model-ParameterBinding

前言

通過(guò)上個(gè)篇幅的學(xué)習(xí)了解Model綁定的基礎(chǔ)知識(shí),然而在ASP.NET Web API中Model綁定功能模塊并不是被直接調(diào)用的,而是要通過(guò)本篇要介紹的內(nèi)容ParameterBinding的一系列對(duì)象對(duì)其進(jìn)行封裝調(diào)用,通過(guò)本篇的學(xué)習(xí)之后也會(huì)大概的清楚在Web API中綁定會(huì)有哪幾種方式。

Model-ParameterBinding(對(duì)象篇)

在ASP.NET Web API中ParameterBinding代表著參數(shù)綁定并且在這其中涉及了幾種綁定的方式,然而ParaMeterBinding并不是單獨(dú)執(zhí)行的,就好比一個(gè)控制器方法中有可能會(huì)有多個(gè)參數(shù)一樣,所以我們就先來(lái)看一下ActionBinding的相關(guān)對(duì)象,對(duì)于這些對(duì)象的生成的環(huán)境以及過(guò)程我們?cè)诤竺娴钠袝?huì)有講解。

HttpActionBinding

代碼1-1

namespace System.Web.Http.Controllers{    public class HttpActionBinding    {        public HttpActionBinding();        public HttpActionBinding(HttpActionDescriptor actionDescriptor, HttpParameterBinding[] bindings);        public HttpActionDescriptor ActionDescriptor { get; set; }        public HttpParameterBinding[] ParameterBindings { get; set; }        public virtual Task ExecuteBindingAsync(HttpActionContext actionContext, CancellationToken cancellationToken);    }}

代碼1-1中對(duì)于HttpActionBinding類(lèi)型的定義一目了然,看HttpActionBinding類(lèi)型的重載構(gòu)造函數(shù)中有HttpActionDescriptor類(lèi)型、HttpParameterBinding類(lèi)型的數(shù)組類(lèi)型作為參數(shù),HttpActionDescriptor類(lèi)型作為控制器方法描述類(lèi)型大家已經(jīng)很熟悉了吧,這個(gè)類(lèi)型中包含著控制器方法的元數(shù)據(jù)信息,而后者HttpParameterBinding類(lèi)型則是表示著參數(shù)綁定對(duì)象,其實(shí)在Model綁定中每個(gè)參數(shù)的綁定都是通過(guò)ParameterBinding來(lái)綁定的,所以這里的執(zhí)行過(guò)程我們暫時(shí)不去深入了解,就是單純的了解一下相關(guān)的對(duì)象模型。

HttpParameterBinding

代碼1-2

namespace System.Web.Http.Controllers{    public abstract class HttpParameterBinding    {        PRotected HttpParameterBinding(HttpParameterDescriptor descriptor);        public HttpParameterDescriptor Descriptor { get; }        public virtual string ErrorMessage { get; }        public bool IsValid { get; }        public virtual bool WillReadBody { get; }        public abstract Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken);        protected object GetValue(HttpActionContext actionContext);        protected void SetValue(HttpActionContext actionContext, object value);    }}

在代碼1-2中我們看到HttpParameterBinding類(lèi)型的定義,可以看到HttpParameterBinding是抽象類(lèi)型,并且實(shí)現(xiàn)綁定的方法ExecuteBindingAsync()也是抽象的,這也是為了在不同的環(huán)境和情況相愛(ài)采取不同的綁定機(jī)制做好鋪墊就是多態(tài)啦,這個(gè)我們后面會(huì)說(shuō),我們還是先看下HttpParameterBinding類(lèi)型的內(nèi)部定義,首先我們看到的是構(gòu)造函數(shù)中的參數(shù)類(lèi)型,HttpParameterDescriptor類(lèi)型,又是一個(gè)對(duì)象描述類(lèi)型,這次的描述對(duì)象則是控制其方法中的某個(gè)參數(shù),而HttpParameterBinding對(duì)象實(shí)例的生成則是根據(jù)HttpParameterDescriptor對(duì)象實(shí)例而來(lái),這個(gè)后續(xù)的篇幅中會(huì)有講解。ErrorMessage屬性表示在ParameterBinding綁定的過(guò)程中出現(xiàn)的錯(cuò)誤信息,而IsValid屬性表示綁定是否可以完成的依據(jù)就是判斷ErrorMessage屬性是否為Null,WillReadBody屬性表示ParameterBinding對(duì)象是否讀取Http消息正文內(nèi)容作為參數(shù)綁定的數(shù)據(jù)源,這個(gè)稍后給大家看的示例中就有,一看便知。GetValue()和SetValue()方法就是向當(dāng)前HttpActionContext中獲取對(duì)應(yīng)的參數(shù)值和設(shè)置參數(shù)對(duì)應(yīng)值。

ModelBinderParameterBinding

代碼1-3

namespace System.Web.Http.ModelBinding{    public class ModelBinderParameterBinding : HttpParameterBinding, IValueProviderParameterBinding    {        public ModelBinderParameterBinding(HttpParameterDescriptor descriptor, IModelBinder modelBinder, IEnumerable<System.Web.Http.ValueProviders.ValueProviderFactory> valueProviderFactories);        public IModelBinder Binder { get; }        public IEnumerable<System.Web.Http.ValueProviders.ValueProviderFactory> ValueProviderFactories { get; }        public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken);    }}

代碼1-3表示的是ModelBinderParameterBinding類(lèi)型,這個(gè)類(lèi)型代表的綁定方式(綁定的數(shù)據(jù)來(lái)源)是通過(guò)IModelBinder來(lái)獲取的,也就正如代碼1-3中我們看到的構(gòu)造函數(shù)一樣,其中有IModelBinder和ValueProviderFactory類(lèi)型的集合,這些就是數(shù)據(jù)綁定的數(shù)據(jù)源。Binder屬性和ValueProviderFactories屬性也就是構(gòu)造函數(shù)傳入的兩個(gè)類(lèi)型值。對(duì)于綁定的細(xì)節(jié)這里就不說(shuō)多了。

FormatterParameterBinding

代碼1-4

namespace System.Web.Http.ModelBinding{    public class FormatterParameterBinding : HttpParameterBinding    {        public FormatterParameterBinding(HttpParameterDescriptor descriptor, IEnumerable<MediaTypeFormatter> formatters, IBodyModelValidator bodyModelValidator);        public IBodyModelValidator BodyModelValidator { get; set; }        public override string ErrorMessage { get; }        public IEnumerable<MediaTypeFormatter> Formatters { get; set; }        public override bool WillReadBody { get; }        public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken);        public virtual Task<object> ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable<MediaTypeFormatter> formatters, IFormatterLogger formatterLogger);    }}

代碼1-4中所示的FormatterParameterBinding類(lèi)型是通過(guò)哪種方式來(lái)獲取綁定數(shù)據(jù)源的呢?大家可以看到WillReadBody屬性在這個(gè)類(lèi)型中被重寫(xiě)了,原來(lái)在HttpParameterBinding類(lèi)型中默認(rèn)為false的是不會(huì)對(duì)Http請(qǐng)求的正文內(nèi)容進(jìn)行讀寫(xiě)的,而這里在FormatterParameterBinding類(lèi)型中,已然的重寫(xiě)了,說(shuō)明在這個(gè)類(lèi)型中我們所能用到的綁定數(shù)據(jù)源就是從Http請(qǐng)求的正文內(nèi)容來(lái)讀取了,然而Http請(qǐng)求的正文內(nèi)容并不是直接放在那里供我們讀取的,而是在客戶(hù)端發(fā)送前就已經(jīng)被指定的序列化器序列化了。

那么我們?cè)诜?wù)器端就要反序列化才能獲取到值,這里在代碼1-4中我們看到構(gòu)造函數(shù)中可以看到和代碼1-3一樣都是具有一個(gè)HttpParameterDescriptor類(lèi)型的參數(shù)對(duì)象,而后則是一個(gè)MediaTypeFormatter類(lèi)型的集合對(duì)象,最后是進(jìn)行Model驗(yàn)證的對(duì)象,這個(gè)后續(xù)再說(shuō)。

現(xiàn)在我們就來(lái)看看第二個(gè)參數(shù)類(lèi)型中的MediaTypeFormatter類(lèi)型。

MediaTypeFormatter

代碼1-5

namespace System.Net.Http.Formatting{    public abstract class MediaTypeFormatter    {        protected MediaTypeFormatter();        public abstract bool CanReadType(Type type);        public abstract bool CanWriteType(Type type);        public virtual Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger);        public virtual Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext);    }}

在代碼1-5中所示的MediaTypeFormatter類(lèi)型是被刪減過(guò)的一部分,所剩4個(gè)方法中兩個(gè)是抽象方法兩個(gè)是虛方法,先看兩個(gè)抽象方法的定義,CanReadType()方法表示的是根據(jù)指定的參數(shù)類(lèi)型從當(dāng)前的Http請(qǐng)求正文中能否讀取到改類(lèi)型的值,簡(jiǎn)單來(lái)說(shuō)就是能否把正文內(nèi)容反序列化為指定的參數(shù)類(lèi)型,同理CanWriterType()是對(duì)響應(yīng)正文進(jìn)行操作判斷。而ReadFromStreamAsync()方法和WriteToStreamAsync()方法則是對(duì)Http請(qǐng)求正文內(nèi)容和Http響應(yīng)正文內(nèi)容進(jìn)行實(shí)際操作的兩個(gè)方法。

對(duì)于MediaTypeFormatter類(lèi)型的實(shí)現(xiàn)類(lèi)型比如說(shuō)針對(duì)Json格式的JsonMediaTypeFormatter和針對(duì)xml格式的XmlMediaTypeFormatter.

我們看一下JsonMediaTypeFormatter類(lèi)型的簡(jiǎn)單示例,大家就會(huì)知道是怎么回事了。

首先我們?cè)诜?wù)器端的控制器中定義一個(gè)接收Post請(qǐng)求的方法,

代碼1-6

        [CustomControllerActionAuthorizationFilter]        public void Post(Product product)        {            products.Add(product);        }

為了方便演示在其控制其方法上加了個(gè)授權(quán)過(guò)濾器,然后我們對(duì)Post請(qǐng)求的處理使用JsonMediaTypeFormatter類(lèi)型進(jìn)行反序列化的操作將在CustomControllerActionAuthorizationFilter過(guò)濾器類(lèi)型中進(jìn)行。

代碼1-7

public Task<System.Net.Http.HttpResponseMessage> ExecuteAuthorizationFilterAsync(System.Web.Http.Controllers.HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken, Func<Task<System.Net.Http.HttpResponseMessage>> continuation)        {            Console.WriteLine(actionContext.Request.Content.Headers.ContentType.MediaType);            IEnumerable<MediaTypeFormatter> formatters = new MediaTypeFormatter[]             {                new JsonMediaTypeFormatter()            };            Product product = actionContext.Request.Content.ReadAsAsync<Common.Product>(formatters).Result;            Console.WriteLine(product.ProductID);            Console.WriteLine(product.ProductName);            Console.WriteLine(product.ProductCategory);            return continuation();        }

在代碼1-7中,在請(qǐng)求到達(dá)控制器方法之前會(huì)先經(jīng)過(guò)授權(quán)過(guò)濾器,在這其中首先我們是先向服務(wù)器端的控制器輸出了當(dāng)前請(qǐng)求的格式類(lèi)型,然后就對(duì)當(dāng)前請(qǐng)求的正文內(nèi)容使用JsonMediaTypeFormatter類(lèi)型進(jìn)行反序列化操作。

我們?cè)倏聪驴蛻?hù)端使用HttpClient類(lèi)型進(jìn)行模擬Post請(qǐng)求,

代碼1-8

            HttpClient httpClient = new HttpClient();            Product product = new Product()            {                ProductID = "003",                ProductName = "旺仔牛奶",                ProductCategory = "食品類(lèi)"            };            await httpClient.PostAsJsonAsync<Product>("http://localhost/selfhost/api/product", product);

最后結(jié)果如圖1.

圖1

看到這里想必大家也知道對(duì)于XmlMediaTypeFormatter的使用方式是怎樣的了。

還有一種MediaTypeFormatter類(lèi)型FormUrlEncodedMediaTypeFormatter類(lèi)型,F(xiàn)ormUrlEncodedMediaTypeFormatter類(lèi)型表示的是在Http Post請(qǐng)求中表單提交的數(shù)據(jù)所用格式器,我們看一下FormUrlEncodedMediaTypeFormatter類(lèi)型中CanReadType()方法的實(shí)現(xiàn)。

代碼1-9


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 日韩激情一区 | 国产精品自在线拍 | 亚洲片在线观看 | 羞羞漫画无遮挡观看 | 欧美日韩在线播放 | 亚洲午夜在线观看 | 在线天堂资源 | 久久99精品久久久久久国产越南 | www国产成人免费观看视频 | 蜜桃网在线观看 | 亚洲小视频在线播放 | 欧美一级特黄aaaaaa在线看首页 | 红桃一区 | 日韩欧美高清一区 | 国产精品刺激对白麻豆99 | 永久免费黄色片 | 亚洲综合视频网 | 99在线热视频 | mmmwww| 日韩视频一区二区三区四区 | 国产va在线观看免费 | 日韩av官网 | 欧美大屁股精品毛片视频 | 日本中文字幕久久 | 一边吃奶一边插下面 | 亚洲日本高清 | 国产免费观看一区二区三区 | 欧美人与zoxxxx另类9 | 国产视频在线观看免费 | 国产青草视频在线观看视频 | 久久久久一区二区三区四区五区 | 最近国产中文字幕 | 国产小视频在线观看 | 免费国产视频大全入口 | 国产成人精品区一区二区不卡 | 91成人免费视频 | 爱操成人网 | 精品一区二区免费视频视频 | 一本色道久久综合狠狠躁篇适合什么人看 | 亚洲国产一区二区三区 | av手机在线免费播放 |