Arr*_*n S 24 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" });
Run Code Online (Sandbox Code Playgroud)
这是我的控制器方法
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetPizzaById(long pizzaKey)
[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult DeletePizza(long pizzaKey)
Run Code Online (Sandbox Code Playgroud)
当我做一个GET它返回对象,但当我做一个删除我得到一个404.看起来这应该工作,但事实并非如此.
如果我切换两条路线然后我可以做DELETE,但在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" }) });
Run Code Online (Sandbox Code Playgroud)
wom*_*omp 19
您可以通过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);
Run Code Online (Sandbox Code Playgroud)
现在你可以拥有两条路线,并且它们将受到动词的约束.
Lev*_*evi 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) { }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7707 次 |
| 最近记录: |