Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

asp.net mvc - How to route GET and DELETE requests for the same url to different controller methods

Here are my routes in Global.asax

    routes.MapRoute("PizzaGet", "pizza/{pizzaKey}", new { controller = "Pizza", action = "GetPizzaById" });
    routes.MapRoute("DeletePizza", "pizza/{pizzaKey}", new { controller = "Pizza", action = "DeletePizza" });    

Here are the my controller methods

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetPizzaById(long pizzaKey)

[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult DeletePizza(long pizzaKey)

When I do a GET it returns the object, but when I do a DELETE I get a 404. It seems like this should work, but it doesn't.

If I switch the two routes around then I can do the DELETE, but get a 404 on the GET.

Now this is truly beautiful. Thanks

routes.MapRoute("Pizza-GET","pizza/{pizzaKey}",
              new { controller = "Pizza", action = "GetPizza"},
              new { httpMethod = new HttpMethodConstraint(new string[]{"GET"})});

            routes.MapRoute("Pizza-UPDATE", "pizza/{pizzaKey}",
              new { controller = "Pizza", action = "UpdatePizza" },
              new { httpMethod = new HttpMethodConstraint(new string[] { "PUT" }) });

            routes.MapRoute("Pizza-DELETE", "pizza/{pizzaKey}",
              new { controller = "Pizza", action = "DeletePizza" },
              new { httpMethod = new HttpMethodConstraint(new string[] { "DELETE" }) });

            routes.MapRoute("Pizza-ADD", "pizza/",
              new { controller = "Pizza", action = "AddPizza" },
              new { httpMethod = new HttpMethodConstraint(new string[] { "POST" }) });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
[ActionName("Pizza"), HttpPost]
public ActionResult Pizza_Post(int theParameter) { }

[ActionName("Pizza"), HttpGet]
public ActionResult Pizza_Get(int theParameter) { }

[ActionName("Pizza"), HttpHut]
public ActionResult Pizza_Hut(int theParameter) { }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...