2
Answers

How to enable cors in asp.net web Method

Ajay Gupta

Ajay Gupta

7y
669
1
Below code sample which i have implemented.
 
Global.asax 
  1. protected void Application_BeginRequest(object sender, EventArgs e)  
  2.         {  
  3.             var context = HttpContext.Current;  
  4.             var response = context.Response;  
  5.   
  6.             // enable CORS  
  7.             response.AddHeader("Access-Control-Allow-Origin""*");  
  8.   
  9.             if (context.Request.HttpMethod == "OPTIONS")  
  10.             {  
  11.                 response.AddHeader("Access-Control-Allow-Methods""GET, POST, OPTIONS");  
  12.                 response.AddHeader("Access-Control-Allow-Headers""Content-Type, Accept");  
  13.                 response.End();  
  14.             }  
  15.         }  
 Web.config
  1. <system.webServer>  
  2.     <httpProtocol>  
  3.       <customHeaders>  
  4.        <add name="Access-Control-Allow-Headers" value="accept, content-type" />  
  5.         <add name="Access-Control-Allow-Origin" value="*" />  
  6.         <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />  
  7.       </customHeaders>  
  8.     </httpProtocol>  
  9.   </system.webServer>  
 

Answers (2)