I am a little desperate about this problem. I am new to angular 2 and .net and I am trying to create a simple application. I programmed api rest in C #. When I call the GET method from angular, it works fine, but not the POST method. I get 405 (method not allowed) all the time, but if I call a post with a postman, everything works. I see many problems with the same problem, but they do not work for me. I have CORS enabled. Here is my code:
Angular
sendPtipo(delegacion,municipio,ejercicio,recinto,tipo){
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
let urlPtipo ='http://localhost:50790/ApiProductoTipo/CalculaPTRecinto';
let body ='delegacion='+delegacion+'&municipio='+municipio+'&recinto='+recinto+'&ejercicio='+ejercicio+'&tipo'+tipo;
return this._http.post(urlPtipo , body , options)
.map(data => {alert('ok');})
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || {};
}
private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}}
Api is resting in C #
using System.Collections.Generic;
using System.Web.Http;
using AppName.Models;
using AppName.Service;
using System.Linq;
using AppName.ViewModels;
using System.Net.Http;
using System.Net;
using System.Web.Http.Cors;
namespace Api.Services
{
public class ApiProductoTipoController : ApiController
{
private readonly IProductoTipoService productoTipoService;
public HttpResponseMessage Options()
{
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}
public ApiProductoTipoController(IProductoTipoService productoTipoService)
{
this.productoTipoService = productoTipoService;
}
[HttpPost]
[Route("~/ApiProductoTipo/CalculaPTRecinto")]
public HttpResponseMessage CalculaPTRecinto([FromBody]int delegacion, int municipio, int ninterno, int ejercicio, string tipo)
{
if (this.productoTipoService.CalculaPTRecinto(delegacion, municipio, ninterno, ejercicio, tipo) != 0)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
}}
webapiconfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Cors;
namespace Web
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "accept,accesstoken,authorization,cache-control,pragma,content-type,origin", "GET,PUT,POST,DELETE,TRACE,HEAD,OPTIONS");
config.EnableCors(cors);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional }
);
}
}
}
Magazine
headers:Headers
ok:false
status:405
statusText:"Method Not Allowed"
type:2
url:"http://localhost:50790/ApiProductoTipo/CalculaPTRecinto"
_body:"{"Message":"The requested resource does not support http method 'POST'."}"
Any idea? Thanks for reading!
Edit: this was a Postman 200 OK answer
Cache-Control →no-cache
Content-Length →0
Date →Fri, 26 May 2017 09:48:05 GMT
Expires →-1
Pragma →no-cache
Server →Microsoft-IIS/10.0
X-AspNet-Version →4.0.30319
X-Powered-By →ASP.NET
X-SourceFiles →=?UTF-8?B?QzpcVXNlcnNcNzAyNTU3MjFKXERlc2t0b3BcQXBpUHJvZHVjdG9UaXBvXFdlYlxBcGlQcm9kdWN0b1RpcG9cQ2FsY3VsYVBUUmVjaW50bw==?=