如何将对同一URL的GET和DELETE请求路由到不同的控制器方法

时间:2010-01-27 01:06:11

标签: asp.net-mvc asp.net-mvc-routing

以下是我在Global.asax

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

以下是我的控制器方法

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

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

当我执行GET时它会返回对象,但是当我执行DELETE时,我会得到一个404.看起来这应该可行,但事实并非如此。

如果我切换两条路线然后我可以进行删除,但是在GET上获得404.

现在这真的很美。感谢

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" }) });

3 个答案:

答案 0 :(得分:18)

您可以通过HTTP动词约束您的路线,如下所示:

  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);

现在你可以拥有两条路线,它们将受到动词的限制。

答案 1 :(得分:18)

[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) { }

答案 2 :(得分:0)

Womp的解决方案对我不起作用。

这样做:

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;
        }
    }
}

如果其他人根据请求方法遇到不同路线的问题。