I'm working on an MVC app and I'm trying to make a AJAX connection to an web API controller I have. The only error I can surface is "Unidentified". Also In my api controller delete action I'm throwing a random exception so I can detect if I successfully get into the method but I don't.
UPDATE:
Ok It seems I was forgetting the "removeFile" route on my url in the ajax call but the issue is If I add on a "data" attribute to the ajax call to pass in my serialized form controls it stops working properly.
data: $("#frmManipulate").serialize()
Ajax call:
$("#frmManipulate").submit(function () {
if (confirm("Are you sure you want to delete all selected files?")) {
if ($(this).data('clicked').is('[name="delete"]')) {
$.ajax({
type: "DELETE",
url: "api/file/removeFile",
data: $("#frmManipulate").serialize()
}).complete(function ($data) {
alert("done");
});
}
return false;
});
Web API Controller:
[RoutePrefix("api/file")]
public class ManipulateController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
// POST api/<controller>
public void Post([FromBody]string value)
{
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
[Route("removeFile")]
// DELETE api/<controller>/5
public void DeleteFile(string[] url)
{
foreach (var file in url)
{
throw new MarshalDirectiveException();
}
}
}