• webapi发布常见错误及解决方案


    webapi发布常见错误及解决方案

    错误一:

    错误:404 (Not Found)

    解决方案:

    在  <system.webServer>节点中添加如下模块:

    <modules runAllManagedModulesForAllRequests="true" />

    错误二:

    错误:405 (Method Not Allowed)或者Response for preflight has invalid HTTP status code 405.

    解决方案:(两个条件同时满足才可以)

    webapi配置文件:

    <modules runAllManagedModulesForAllRequests="true" />  这个是核心,其他的都可以没有这个是必须的(但是安全方面考虑肯定是不可以的,然后通过如下方案方式解决)

    2.后端处理:

    第一种方案:在控制器中对应的方法都增加一个支持OPTIONS请求的方法,返回202即可;accepted就是202

    [HttpOptions]
            public HttpResponseMessage Login()
            {
                return new HttpResponseMessage(HttpStatusCode.Accepted);
            }
            /// <summary>
            /// 用户登陆
            /// </summary>
            /// <param name="model"></param>
            /// <returns></returns>
            [HttpPost]
            public HttpResponseMessage Login([FromBody] LoginModel model)
            {
                var cache = HttpRuntime.Cache;
                var user = cache.Get(model.Account) as UserModel;
                if (user == null)
                {
                    return Error(1, "用户未注册!");
                }
                if (user.Password != model.Password)
                {
                    return Error(1, "用户名或密码错误!");
                }
                return Success(0, "登陆成功!", user);
            }

    第二种方案:在Global文件中增加如下这个方法:(推荐使用)

     protected void Application_BeginRequest(object sender, EventArgs e)
            {
                var res = HttpContext.Current.Response;
                var req = HttpContext.Current.Request;
    
                //自定义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,Authorization");
                    //res.AppendHeader("Access-Control-Allow-Methods", "POST,GET,PUT,PATCH,DELETE,OPTIONS");
                    res.StatusCode = 202;
                    res.End();
                }
            }

     如上如果不可以,可以尝试如下方案:

    在  <system.webServer>节点中添加如下模块:

    <modules runAllManagedModulesForAllRequests="true" />

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

    <modules runAllManagedModulesForAllRequests="true" />

    <handlers>
                 <remove name="WebDAV" />
                 <remove name="WebAPI_64bit" />
                 <remove name="WebAPI_32bit" />
                 <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
                 <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
                 <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
                 <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="*" resourceType="Unspecified" requireAccess="Script" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFrameworkv4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
                 <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="*" resourceType="Unspecified" requireAccess="Script" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
                 <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" resourceType="Unspecified" requireAccess="Script" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
                 <add name="WebAPI_64bit" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv4.0,bitness64" />
                 <add name="WebAPI_32bit" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFrameworkv4.0.30319aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv4.0,bitness32" />
             </handlers>

  • 相关阅读:
    [Tomcat源码分析] Eclipse中搭建Apache Tomcat源码调试环境
    java基本数据类型
    JD-GUI反编译出现ERROR
    Convert.ToInt32()与int.Parse()的区别 (转载)
    C#编写记事本(高仿)
    少年三国志首页效果实现
    Linux系统下文件删除的恢复方法
    prefab 程序自动写回
    Unity3d UI与逻辑分离改造
    在windows上练习object-c
  • 原文地址:https://www.cnblogs.com/zlp520/p/9436645.html
Copyright © 2020-2023  润新知