【问题标题】:Strange 405 errors against web api methods针对 web api 方法的奇怪 405 错误
【发布时间】:2015-08-01 04:31:09
【问题描述】:

所以我真的在这个问题上苦苦挣扎,并搜索了互联网并尝试了我能找到的所有可能的建议。

我有一个 web api c# 项目和一个 JS web 项目。在本地运行时,一切正常。当我发布到 Azure 时,我可以通过 REST 控制台访问我的 api 方法,一切正常。问题是当我的 Web 应用程序尝试对每个方法(初始令牌调用除外)进行相同的调用时,都会返回 405。

我已经多次使用 CORS 设置和 web.config,我不确定是否需要什么...这变得非常混乱,但我会尝试在此处粘贴相关位。

我的 web.config system.webServer 设置

<modules>
  <remove name="FormsAuthentication" />
  <remove name="WebDAVModule"/>
</modules>

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
  </customHeaders>
</httpProtocol>

<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>

WebApiConfig.cs 注册方法

        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

还有一个示例 api 控制器

    [Authorize]
    [Route("getall")]
    [HttpGet]
    public IHttpActionResult GetAll()
    {
        return Ok(PersonRepository.Get());
    }

还有 JS $http.get($rootScope.serviceBase + 'api/person/getall')

所以总结一下这里的情况:

  • 在本地运行时,调用工作正常
  • 发布到 Azure 后,我可以直接调用其余端点
  • 发布到 Azure 时,我的 JS 返回 405 非描述性错误
  • 如果我从 web.config 中删除 CORS 标头并在我的 Register 方法中使用 enableCors,我什至无法点击 /token,而是到处都收到 CORS 预检错误(尽管我仍然可以成功地从休息控制台进行调用)这非常令人困惑

更新

我会记录我在这里尝试的东西的运行日志:

  1. 在 webConfig 中的 customHeaders 中添加了 &lt;add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /&gt;,没有变化

【问题讨论】:

    标签: c# api rest azure asp.net-web-api


    【解决方案1】:

    已修复!感谢各种 stackoverflow 帖子的汇编(我喜欢这个网站),我重建了我的 web.config

    如果它对其他人有用,这是我的新 web.config

    <system.web>
    
    
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
    
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <customErrors mode="Off" />
    </system.web>  
    <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE" />
        <add name="Access-Control-Allow-Headers" value="Content-Type,Authorization" />
      </customHeaders>
    </httpProtocol>
    <modules runAllManagedModulesForAllRequests="false">
      <remove name="WebDAVModule" />
    </modules>
    
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <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="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
    
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    
    </handlers>
    
    </system.webServer>
    

    【讨论】:

    • 如果您解释了您必须进行的具体更改,将会很有帮助。
    猜你喜欢
    • 2017-10-18
    • 2017-11-30
    • 2015-04-07
    • 1970-01-01
    • 2018-06-02
    • 2015-06-20
    • 2014-12-21
    • 2013-03-21
    • 1970-01-01
    相关资源
    最近更新 更多