Chirag
Chirag

Reputation: 1851

Angular2 post and put requests not calling web api methods

I have a RegionService class in Angular 2, which has Add and Update Region methods. However, the post and put methods does not reach respective methods in Web API. I put the debug breakpoint in Web API methods but it doesn't reach there.

Here is the methods in RegionService class:

enter image description here Here is the code for RegionApiController with post and put methods:

enter image description here

However, If I use postman to submit post and put, it stops at breakpoint in Web API methods and able to add/update region record successfully.

enter image description here

Please help!

Upvotes: 1

Views: 1074

Answers (1)

Chirag
Chirag

Reputation: 1851

Following steps resolved the issues for me and I was able to get response from Web Api hosted on Server.

1) Added following NuGet packages for CORS

  <package id="Microsoft.AspNet.Cors" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Cors" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />

2) Enabled CORS in WebApiConfig.cs file within WebAPI application -> inside Register() method

var enableCorsAttribute = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(enableCorsAttribute);

Note: the localhost url that you see in the question image was actually url hosted on server that I changed in my code prior to posting question here...

Hope this helps someone...

Upvotes: 2

Related Questions