ASP. NET MVC 5 Web API about requested resource does not the support options issue

Source: Internet
Author: User

1. Build a Web API application with Visual Studio 2015. Remember that this is a Web API app.

2. Create a new Web API.

3, in C # Access, the code is as follows: [no problem, return correct]

varRequestjson =jsonconvert.serializeobject (args); Httpcontent httpcontent=Newstringcontent (Requestjson); HttpContent.Headers.ContentType=NewMediatypeheadervalue ("Application/json");varHttpClient =NewHttpClient ();varResponsejson = Httpclient.postasync ("http://webml01.dxqas.com/datacenter/api/affiliate/queryproduct", Httpcontent). Result.Content.ReadAsStringAsync (). Result;

But if you use jquery to invoke the Web API, it will appear:

  <Error><Message>The requested resource does not support http method ‘OPTIONS‘.</Message></Error>

解决方法如下:

1、编写一个 web api 的基类如 BaseApi.cs 继承自 ApiController 在BaseApi.cs中添加如下代码
 Public httpresponsemessage Options ()        {            returnnew httpresponsemessage {StatusCode = Httpstatuscode.ok};        }

2. Add the following configuration to the <system.webServer> node in Web. config

<customHeaders>
<add name= "Access-control-allow-origin" value= "*"/>
<add name= "Access-control-allow-methods" value= "GET, PUT, POST, DELETE, HEAD"/>
<add name= "Access-control-allow-headers" value= "Origin, X-requested-with, Content-type, Accept"/>
</customHeaders>

Complete the above two, then use jquery to call. No further error is given. However, when F12 looks at the request, it will find two requests generated.

ASP. NET MVC 5 Web API about requested resource does not support options issues

Related Article

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.