2
votes

I'm trying to send a post request from an Angular front-end to a ASP.net backend.

Get requests work fine.

When I send:

this.http.post("http://localhost:3000/api/Cards", "some data", httpOptions).subscribe(res => this.display(res));

To the ASP.NET API I'm running from VS2017 I get:

ERROR {…}​error: Object { Message: "The requested resource does not support http method 'POST'." }​headers: Object { normalizedNames: Map(0), lazyUpdate: null, lazyInit: lazyInit() }​message: "Http failure response for http://localhost:3000/api/Cards: 405 Method Not Allowed"​name: "HttpErrorResponse"​ok: false​status: 405​statusText: "Method Not Allowed"​url: "http://localhost:3000/api/Cards"​: Object { constructor: HttpErrorResponse() } core.js:12584

For Angular, "httpOptions" consists of:

const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'text/plain', 'accept' : 'text/plain' })};

My WebApi.Config:

<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Headers" value="Content-type"/>
        <add name="Access-Control-Allow-Methods" value="POST"/>
        <add name="Access-Control-Allow-Origin" value="*"/>
      </customHeaders>
    </httpProtocol>
    <handlers>
      <remove name="WebDAV"/>
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web,Version=2.0.0.0, Culture=neutral,PublicKeyToken=<REMOVED>"/>
      <add name="UrlRoutingModule-4.0" path="*" verb="*" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
      <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">
      <remove name="TelemetryCorrelationHttpModule" />
      <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
      <remove name="WebDAVModule"/>
      <remove name="UrlRoutingModule"/>
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1" />
      </dependentAssembly>      
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

It may look very messy because I've tried solutions from lots of different Stack Overflow questions and asp.net forum posts. Nothing seems to get rid of the 405 error.

WebApiConfig.cs:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        config.EnableCors();
        // Web API routes
        config.MapHttpAttributeRoutes();

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

Controller:

    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class CardsController : ApiController
    {
        // Test data set
        private Card[] cards = new Card[]
        {
            new Card {Id = 1, Suit = "D", Value = "7"},
            new Card {Id = 1, Suit = "D", Value = "K"},
            new Card {Id = 1, Suit = "S", Value = "A"},
            new Card {Id = 1, Suit = "H", Value = "2"},
            new Card {Id = 1, Suit = "S", Value = "10"}
        };

        // GET: api/Cards
        [ResponseType(typeof(IEnumerable<Card>))]
        public IEnumerable<Card> Get()
        {
            return cards;
        }

        // GET: api/Cards/5
        public string Get(int id)
        {
            return "value";
        }


         // POST: api/Cards
         public HttpResponseMessage Post(string value)
         {
             return Request.CreateResponse("You found me!");
         }


        // PUT: api/Cards/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/Cards/5
        public void Delete(int id)
        {
        }
    }

Let me know if I left anything important out.

3
Try putting [HttpPost] above your Post action method.Gabriel Luci
This is my new post method, I'm still getting the 405 error though. [HttpPost] public HttpResponseMessage Post(string value) { return Request.CreateResponse("You found me!"); }Raphiell
What happens when you call: this.http.post("http://localhost:3000/api/Cards", { value: "some data" }, httpOptions).subscribe(res => this.display(res)); Since you are posting, it's probably serializing "some data" into an object that doesn't match your string value on the endpoint.jonsca
I changed it to the above, still getting the 405 error. I'm pretty sure my request is ok, I tested it with ptsv2.com. Thanks though.Raphiell

3 Answers

0
votes

you need to tell the controller action it is a post
add the declaration above the action.

[HttpPost]
public ActionResult MyAction(){}

above your controller action.

0
votes

You were supposed to add [HttpPost] on your endpoint in the Asp.net as below:

[HttpPost] public ActionResult MyAction() { }

-5
votes

I switched to using Ruby on Rails as a backend.