This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21428|Reply: 1

[WebAPI] Problems and solutions for AJAX cross-domain calls to ASP.NET MVC or WebAPI services

[Copy link]
Posted on 12/1/2015 3:20:20 PM | | | |
Problem description
When a cross-domain call ASP.NET MVC or a service written ASP.NET Web API, it becomes inaccessible.
Reproduction method
  • Use a template to create a simplest ASP.NET Web API project, debug it to make sure it works
  • Create another project with only one HTML page and make an AJAX call
  • Open this page in your browser and you will find the following error (405: Method Not Allowed)

    Note: The same situation occurs in ASP.NET MVC. In some cases, MVC can also be used directly to develop services, which has its own advantages and disadvantages compared to WebAPI. Below is an example of a service developed using MVC


Cause analysis
The fundamental reason for the cross-domain problem is that the browser has low privileges on both requests and usually only allows calls to resources in the local domain, unless the target server explicitly tells it that cross-domain calls are allowed.
Therefore, although the cross-domain problem is caused by the behavior of the browser, the solution is on the server side. Because it is not possible to require all clients to reduce security.

solution
For both ASP.NET MVC and ASP.NET Web API project types, I did some research and determined that the following scenario is feasible.
For ASP.NET MVC, you only need to add the following content to the web.config


For ASP.NET Web APIs, in addition to the above settings, a special design needs to be added, which is to add an OPTIONS method for each API Controller, but without returning anything.
public string Options()
{
return null; // HTTP 200 response with empty body
}

Note: This function can also be done with some research, and it may be better to design it as a filter.





Previous:Lenovo's official channel notebook quotation on December 1
Next:Implement a simple Http service with HttpListener
 Landlord| Posted on 12/1/2015 3:39:52 PM |
  1. <system.webServer>

  2. <httpProtocol>

  3. <customHeaders>

  4. <add name="Access-Control-Allow-Origin" value="*" />

  5. <add name="Access-Control-Allow-Headers" value="Content-Type" />

  6. <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />

  7. </customHeaders>

  8. </httpProtocol>

  9. <handlers>

  10. <remove name="ExtensionlessUrlHandler-Integrated-4.0" />

  11. <remove name="OPTIONSVerbHandler" />

  12. <remove name="TRACEVerbHandler" />

  13. <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

  14. </handlers>

  15. </system.webServer>
Copy code


Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com