The pit that webapi stepped on

1. webapi cross-domain needs to be added in the <system.webServer></system.webServer> node of web.config

<!--Cross-domain-->
    <httpProtocol>
      <!--Cross-domain configuration start-->
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <!--Support full domain name access, insecure, need to be fixed to the client URL after deployment-->
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
        <!--Supported http actions-->
        <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type" />
        <!--Please add the response header according to your own needs. The headers of the new token are added here-->
        <add name="Access-Control-Request-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
        <!--Allow the requested http action-->
      </customHeaders>
      <!--End of cross-domain configuration-->
    </httpProtocol>
Cross-domain configuration

2. The webapi setting returns json format by default. You need to add the following code to the Register method of the WebApiConfig file

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            //默认返回 json  
            GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
                new QueryStringMapping("datatype", "json", "application/json"));

            // Solve the circular reference problem during json serialization 
            config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
WebApiConfig configuration

3. Post request webapi parameter transfer problem

Move to: https://www.cnblogs.com/landeanfen/p/5337072.html

 

4. When a cross-domain request needs to pass a custom header and the request fails

Cross-domain requests will first send a preprocessing request and then send the actual request. The preprocessing request is of the OPTIONS type, so it needs to be dealt with specially.

(1) Add the following method to the Global.asax file:

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            var res = HttpContext.Current.Response;
            var req = HttpContext.Current.Request;

            // Processing when customizing the header 
            if (req.HttpMethod == " OPTIONS " )
            {
                res.AppendHeader("Access-Control-Allow-Headers", "Content-Type, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name,Token,Cookie");
                res.AppendHeader("Access-Control-Allow-Methods", "POST,GET,PUT,PATCH,DELETE,OPTIONS");
                res.StatusCode = 200;
                res.End();
            }
        }
Preprocessing requests for special handling

(2) Find name="Access-Control-Allow-Headers" in the cross-domain configuration of web.config, and then add your own custom header name to its value, as shown below

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326043689&siteId=291194637
Recommended