Strange 405 errors against web api methods

时间:2015-07-31 20:03:24

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

So I'm really struggling with this one and have scoured the internets and tried every possible suggestion I could find.

I have a web api c# project and a JS web project. When running locally everything works fine. When I publish to Azure I can hit my api methods via a REST console and everything works fine. The issue is when my web app tries to hit the same calls every method (except from the initial token call) returns a 405.

I have jacked with CORS settings and web.config so many times I'm not sure what is needed anymore or not... it's getting very confusing, but I will try to paste the relevant bits here.

my web.config system.webServer settings

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

        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 }
        );

And an example api controller

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

And the JS $http.get($rootScope.serviceBase + 'api/person/getall')

So to summarize the situation here:

  • When running locally the calls work fine
  • When published to Azure, I can make direct calls to the rest endpoints fine
  • When published to Azure, my JS returns 405 non-descript errors
  • If instead I remove the CORS header from web.config and use enableCors in my Register method I can't even hit /token, instead I get CORS preflight errors everywhere (though I still can successfully make calls from a rest console) which is exceptionally confusing

Updates

I'll keep a running log of the stuff I'm trying here:

  1. Added <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /> to customHeaders in webConfig, no change

1 个答案:

答案 0 :(得分:0)

Fixed! Thanks to a compilation of various stackoverflow posts (I love this site) I rebuilt my web.config

Incase it's of use to anyone else, here is my new 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>