CORS with WebAPI for XmlHttpRequest

前端 未结 5 823
不知归路
不知归路 2020-12-14 04:45

Just wanted to know what is the best elegant way (currently available) to handle CORS (Cross-Origin Resource Sharing) in ASP.NET WebAPI so i can use XmlHtt

5条回答
  •  臣服心动
    2020-12-14 05:21

    Add below in web.cofig file(inside the system.webserver element).

    
          
            
            
            
          
        
    

    and add below code in global.aspx.cs file

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
            {
                Response.StatusCode = 200;
                Response.End();
            }
        }
    

提交回复
热议问题