"ASP. NET Core" filters Action based on Content-type header

Source: Internet
Author: User

Before the start of today's bragging program, old Zhou Xian said: "Every week in Friday or Saturday, the old week will be on the Sina Live platform (a live synchronization) of the launch of ASP-related content." The specific live-time will be posted on Weibo in the old week. Live is free to watch, of course, if you have more money, you can also sponsor the old weeks a few euros.

Well, get into today's theme. Leave a suspense, the old weeks don't tell you what to do, first look at an example.

[Route ("/api/[controller]/[action]")]     Public classDemocontroller:controller {[ActionName ("Send"), HttpGet] PublicIactionresult Send_ver1 () {returnContent ("you are accessing version 1 of the interface, which is provided for brute use. "); } [ActionName ("Send")] [HttpGet] PublicIactionresult Send_ver2 () {returnContent ("you are accessing version 2 of the interface, which is provided to the scum for use. "); }    }

This is a controller class, because no view is returned, or it can be considered a Web API.

Key attention to these two Attribute.

ActionName ("send") actionname ("send")

There are two methods in the class named Send_ver1 and Send_ver2, because I use actionnameattribute on them, and the Action name that is exposed by both methods is called send.

As a result, the question arises, with two actions of the same name, and how the application is chosen at run time.

Come on, try it.

After all, the computer is not a human brain, it does not think, so when running the application is unable to make a choice.

Generally really developed words, no one did so, to get two of the same action dry dick, but, for the next demonstration, the old week decided to do a dick. This time if you have to insist on using the two action with the same name, you can differentiate the two actions by the Content-type Header that is passed when the client invokes it.

So, you can make such a modification to the above code, apply a Consumesattribute, of course, the point is that you have to specify Content-type.

[Route ("/api/[controller]/[action]")]     Public classDemocontroller:controller {[ActionName ("Send"), HttpGet] [Consumes (  "application/pdf")]  PublicIactionresult send_ver1 () {...} [ActionName ("Send")] [HttpGet] [Consumes (  "audio/dv")]  PublicIactionresult send_ver2 () {...} }

After this change, when the request is initiated, if the specified Content-type is application/pdf, then the call goes into the Send_ver1 method, and if the content-type specified at the request is AUDIO/DV, it will enter Send_ Ver2 method.

This consumesattribute can also specify multiple content-type when instantiated. Because it has such a constructor.

 Public Consumesattribute (stringparamsstring[] othercontenttypes)

For example, if you want Content-type with Application/pdf and application/xml to enter the Send_ver1 method, then you can do so.

 [Consumes ("application/pdf""application/xml")]     public iactionresult send_ver1 ()        {            ...

}

Now let's test it again.

The first step is to carry Content-type = Application/pdf Call the Web API.

The second step is to change the Content-type to AUDIO/DV and then initiate a request.

The third step, change the Content-type to Application/xml, try again.

Because we specified above, the content type of application/pdf and Application/xml can enter the Send_ver1 method, so the first step is the same as the result of the third step test.

Well, today's bragging program is finished, the next old week with you continue to pull.

"ASP. NET Core" filters Action based on Content-type header

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.