Error 405 when consuming Api Rest service

1

I am having a problem when trying to consume an Api Rest service from Angular 6, my Angular code is as follows:

GuardarDatos(datos:General):Observable<string> {

const httpOptions = {
    headers: new HttpHeaders({
        'Content-Type':  'application/json',
        'Authorization': 'Access-Control-Allow-Origin'
    })
};
let url ='${Conexion.URL}${Operacion.guardardatos}';
let header =this.getHeaders();
return this.http.post<string>(url,datos,httpOptions).pipe();

I have the ApiRest as follows:

[ActionName("TasaPreferTdc")]
[HttpPost]
public string CrearTasaPrefer([FromBody]DatoGeneral objDatoGeneral)
{ return "";}

When making the call I get this error:

    
asked by GHerreraZ 08.06.2018 в 23:31
source

2 answers

1

enable CORS done the following steps

  • Install the nuget package for CORS : Install-Package Microsoft.AspNet.WebApi.Cors .
  • Modify your WebApiConfig.cs file with the Register method by adding the following line

    config.EnableCors();
    
  • You can enable CORS in your controller in the following way

     using System.Net.Http;
     using System.Web.Http;
     using System.Web.Http.Cors;
    
    namespace CORSWithWebAPI.Controllers
    {
       [EnableCors(origins: "http://client.domain", headers: "*", methods: "*")]
       public class SampleController : ApiController
       {
          public HttpResponseMessage Post() { ... }
    
         [DisableCors]
         public HttpResponseMessage PutItem(int id) { ... }   
       }
    }
    
  • You can enable CORS at action level

    public class SampleController : ApiController
    {
        [EnableCors(origins: "http://client.domain", headers: "*", methods: "*")]
        public HttpResponseMessage GetSample(int id) { ... }
    }
    
  • You can enable it globally by modifying point 1:

    public static class WebApiConfig
    { 
       public static void Register(HttpConfiguration config)
       {
          var cors = new EnableCorsAttribute("client.domain", "*", "*");
          config.EnableCors(cors);
       }
    }
    
  • Even from web.config

                                         

    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Credentials" value="true"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol> 
    

  • answered by 12.06.2018 в 16:03
    0

    Notice that what you are really complaining about is that your server is not accepting a request of type OPTIONS, not the POST that you do with your code.

    That OPTIONS request is sent by the browser as part of the control of cross-originated requests (what in English they call CORS), that is, it is included when requesting a resource to a server that is not the one that has generated the page (in your case, you are serving the page from localhost: 4200 and you are doing the POST to localhost: 49143).

    Check the configuration of your IIS server so that it accepts OPTIONS requests.

    PS: I leave you this link so you can take a look at it

        
    answered by 09.06.2018 в 10:48