user12282 user12282 - 12 days ago 4
Ajax Question

C# Webapi POST request fails - 404 NOT FOUND

I have a webapi Controller class with different action, GET and PUT are working fine, POST is failing the whole time and I am quite frustrated. Maybe you can give me some hints how I can solve this issue.

Controller Action:

[RoutePrefix("api/v1/jobs")]
public class JobsController : ApiController
{
[ActionName("PostInactiveStatus")]
[System.Web.Http.AcceptVerbs("POST")]
[System.Web.Http.HttpPost]
public IHttpActionResult PostInactiveStatus(Job job)
{
CsJobSchedulerEntities dataContext = new CsJobSchedulerEntities();
try
{
var refJob = (from j in dataContext.Job
where j.JOB_ID == job.JOB_ID
select j).SingleOrDefault();

refJob.JOB_ACTIVE = 0;
refJob.JOB_MUSER = job.JOB_MUSER;
refJob.JOB_MDATE = DateTime.Now;

dataContext.SaveChanges();

var jobs = dataContext.Job.ToList();
return Ok(jobs);
}
catch (Exception ex)
{
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, new HttpError(ex.Message)));

}

}
}


WebapiConfig class:

public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();

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

//PAM: Add Default Message Handler when Content-Type is Empty
config.MessageHandlers.Add(new DefaultContentTypeMessageHandler());

//PAM: Web API configuration and services
EnableCrossSiteRequests(config);

}

private static void EnableCrossSiteRequests(HttpConfiguration config)
{
var cors = new EnableCorsAttribute(
origins: "*",
headers: "*",
methods: "*");
config.EnableCors(cors);
}


Client Request:

function removeJob(data) {

jQuery.support.cors = true;
$.ajax({
url: HOST + "api/v1/jobs/PostInactiveStatus",
type: "Post",
data: data,
async: false,
beforeSend: function () {

},
success: function (data) {

},
error: function (xhr, ajaxOptions, thrownError) {

}
});
}


}

Http Request Network request

Error Message:


"Message": "No HTTP resource was found that matches the request URI
http://localhost:59732/api/v1/jobs/PostInactiveStatus
.",
"MessageDetail": "No type was found that matches the controller named
'v1'."


Where I fail?

UPDATE: The Controller has added the RoutePrefix for versioning the API, as stated GET and PUT are working fine.

Answer

Assuming the name of the controller is JobsController and that the route template defined in the OP

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

is accurate, then update the ajax call to match expected route template

url: HOST + "api/jobs/PostInactiveStatus",

Other wise a new route would have to be defined for that url which includes v1 in the route template

config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
    name: "V1Api",
    routeTemplate: "api/v1/{controller}/{action}/{key}",
    defaults: new { key = RouteParameter.Optional }
);

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

UPDATE

based on added information, the route attribute for the action is missing. include it on the action.

[RoutePrefix("api/v1/jobs")]
public class JobsController : ApiController {
    //POST api/v1/jobs/PostInactiveStatus
    [ActionName("PostInactiveStatus")]
    [System.Web.Http.AcceptVerbs("POST")]
    [System.Web.Http.HttpPost]
    [Route("PostInactiveStatus")] //<-- this was missing
    public IHttpActionResult PostInactiveStatus(Job job) { ...}
}