0

I have a ASP.NET MVC app. I have single function pattern which will be called both with HTTP POST and HTTP DELETE.

Although Post is called, Delete is never called. I confirmed that the IIS accepts HTTP Delete. Any comments?

Route and Controllers:

routes.MapHttpRoute(
            name: "RegisterCard",
            routeTemplate: "{version}/cards/{cardID}",
            defaults: new { Controller = "MyController", Action = "

           routes.MapHttpRoute(
           name: "UnregisterCard",
           routeTemplate: "{version}/cards/{cardID}",
           defaults: new { Controller = "MyController", Action = "Delete" });                




    [HttpPost]
    public async Task<HttpResponseMessage> Post(string version, string cardID);
    {
    }

    [HttpDelete]
    public async Task<HttpResponseMessage> Delete(string version, string cardID);
    {
    }
2
  • Which browser(s) are you using? IE 8 and 9 don't do AJAX DELETE/PUT very well. Try adding a "X-HTTP-Method-Override: Delete" header in the request and using POST. Does that trigger the action? Commented Oct 31, 2013 at 17:02
  • Check this answer and go through your config files stackoverflow.com/questions/10906411/… Commented Oct 31, 2013 at 21:21

2 Answers 2

1

From the code above, i think any url with pattern {version}/cards/{cardID} will be handled by "RegisterCard" route no matter what the verb is(Post/Delete). For "Delete", "RegisterCard" route will be chosen, then when [HttpPost] action selector comes into play, it will result in a 404 error. If you are experiencing 404 for "Delete", you might

ONE Add constraint to routes

routes.MapHttpRoute(
    name: "RegisterCard",
    routeTemplate: "{version}/cards/{cardID}",
    defaults: new { Controller = "MyController", Action = "Post"},
    constraints: new { httpMethod = new HttpMethodConstraint(new[] { "post" }) }
);

routes.MapHttpRoute(
    name: "UnregisterCard",
    routeTemplate: "{version}/cards/{cardID}",
    defaults: new { Controller = "MyController", Action = "Delete" },
    constraints: new { httpMethod = new HttpMethodConstraint(new[] { "delete" }) }
); 

OR Make a single route merging them together with a single ActionName

routes.MapHttpRoute(
    name: "Card",
    routeTemplate: "{version}/cards/{cardID}",
    defaults: new { Controller = "MyController", Action = "HandleCard"}
);

[ActionName("HandleCard")]
[HttpPost]
public async Task<HttpResponseMessage> Post(string version, string cardID);
{
}

[ActionName("HandleCard")]
[HttpDelete]
public async Task<HttpResponseMessage> Delete(string version, string cardID);
{
}

hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure that HTTP supports delete. Regardless, Just use post for your delete action. As long as you're not using GET for a DELETE action, you're good. Here's some reference...

http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and-delete-methods

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.