25

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" }) });
Arron S
  • 5,511
  • 7
  • 50
  • 57

3 Answers3

21
[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) { }
Levi
  • 32,628
  • 3
  • 87
  • 88
19

You can constrain your routes by HTTP verb like this:

  string[] allowedMethods = { "GET", "POST" };
  var methodConstraints = new HttpMethodConstraint(allowedMethods);

  Route reportRoute = new Route("pizza/{pizzaKey}", new MvcRouteHandler());
  reportRoute.Constraints = new RouteValueDictionary { { "httpMethod", methodConstraints } };

    routes.Add(reportRoute);

Now you can have both routes, and they will be constrained by the verbs.

casperOne
  • 73,706
  • 19
  • 184
  • 253
womp
  • 115,835
  • 26
  • 236
  • 269
  • 1
    Thanks for the new anwser.. I also found this http://arcware.net/adding-httpmethodconstraint-to-asp-net-mvc-routes/ – Arron S Jan 27 '10 at 02:36
0

Womp's solution did not work for me.

This does:

namespace ...
{
    public class ... : ...
    {
        public override void ...(...)
        {
            context.MapRoute(
                "forSpecificRequestMethod",
                "...{rctrl}/{ract}/{id}",
                new { controller = "controller", action = "action", id = UrlParameter.Optional },
                new RouteValueDictionary(new { httpMethod = new MethodRouteConstraint("REQUEST_VERB_HERE") }),
                new[] { "namespace" }
            );

            context.MapRoute(
                "default",
                "...{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                new[] { "namespace" }
            );
        }
    }

    internal class MethodRouteConstraint : IRouteConstraint
    {
        protected string method;
        public MethodRouteConstraint(string method)
        {
            this.method = method;
        }
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return httpContext.Request.HttpMethod == method;
        }
    }
}

In case anyone else is having issues with having different routes based on request method.

csga5000
  • 4,062
  • 4
  • 39
  • 52