Stack Exchange
post method not getting called in Web API
I have written the method with post attribute as follows but the method is not getting called from angular.I am calling the method from angular side but the method is not getting called.
c# code
[System.Web.Mvc.HttpPost]
    [System.Web.Mvc.Route("ApiWithAction")]
    public HttpResponseMessage GetPreviewCount( string requestorEmail)
    {
        HttpResponseMessage response = new HttpResponseMessage();
        try
        {
            response = Request.CreateResponse(HttpStatusCode.OK, _bookingBusinessService.GetPreviewCount(requestorEmail));
            response = this.SetCacheControlHeaderValue(response);
            return response;
        }
        catch (Exception ex)
        {
            Write(ex);
            HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest);
            httpResponseMessage.Content = new StringContent(ex.Message);
            throw new HttpResponseException(httpResponseMessage);
        }
    }

code in WebApiConfig
public static void Register(HttpConfiguration config)
    {

        config.EnableCors();
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

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

angular code
service ts
 getPreviewCount(requestorEmail: string): Observable<any> {
alert(requestorEmail);
alert(this.getAPI('GetPreviewCount'));
return this.http.post<any>(this.getAPI('GetPreviewCount'), requestorEmail,
  { headers: this.httpOptions.headers, observe: 'response' }).pipe(
    map(data => data),
    catchError(this.handleError<any>('getPreviewCount'))
  );
}

component ts
 getPreviewCount() {
this.previewCountSubscription = this.restService.getPreviewCount(this.currentUserEmail).subscribe(
  data => {
    this.previewCount = data;
  });

}

Where are you caling getPreviewCount()

@AshishRanjan from getPreviewCount() is written service.ts file and i am calling it from component ts file.

Yes, can you please show the code where you call this method?

this.getAPI('GetPreviewCount') is returning a correct URL, right? What do you see in the networks tab of your browser when you call getPreviewCount()

http://localhost:54449/api/IndividualBooking/GetPreviewCount

have you check that your config.MapHttpAttributeRoutes(); is called?

i  thing  this.getAPI('GetPreviewCount') should be this.getAPI('ApiWithAction')  as being set on route attribute on the GetPreviewCount api method

and  also try this  : return this.http.post(this.getAPI('GetPreviewCount'), {requestorEmail:requestorEmail},
{ headers: this.httpOptions.headers, observe: 'response' }).pipe(
map(data => data),
catchError(this.handleError('getPreviewCount'))
);
} by  sending the parameter with  the att  name {requestorEmail:requestorEmail}
