【问题标题】:Route URI to Different Classes Based On Method根据方法将 URI 路由到不同的类
【发布时间】:2014-02-16 06:05:07
【问题描述】:

我想根据 HTTP 方法是 GET 还是 POST 将相同的 URI 路由到两个不同的类。我尝试使用RouteAttribute 执行此操作,但两条路线都不起作用。如果我删除一个,另一个工作。如果我将这些方法放在同一个类中,它也可以工作。我也试过RouteAttribute + HttpGetAttributeRouteAttribute + HttpPostAttribute。有没有办法将方法分成两个不同的类?

【问题讨论】:

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


    【解决方案1】:

    我认为您应该尝试在路由声明中使用HttpMethodConstraint,代码将是这样的:

    routes.MapRoute(
        "route for the GET verb",
        "url",
        new { controller = "controller", action = "action" },
        new { httpMethod = new HttpMethodConstraint("GET") }
    );
    
    routes.MapRoute(
        "route for POST verb",
        "url",
        new { controller = "other controller", action = "other action" },
        new { httpMethod = new HttpMethodConstraint("POST") }
    );
    

    【讨论】:

      猜你喜欢
      • 2011-01-29
      • 1970-01-01
      • 2011-06-05
      • 2019-07-12
      • 2020-01-03
      • 2023-03-05
      • 2020-04-16
      • 1970-01-01
      • 2018-07-28
      相关资源
      最近更新 更多