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