简体   繁体   English

在ASP.NET Web Api中以编程方式创建GET路由

[英]Programmatically create GET route in ASP.NET Web Api

I'm trying to programmatically create a route in ASP.NET Web Api that s 我正在尝试以编程方式在ASP.NET Web Api中创建一个路由

config.Routes.MapHttpRoute(
   name: "test",
   routeTemplate: "api/foo",
   defaults: new
   {
       controller = "Foo",
       action = "Test"
   },
   constraints: new
   {
         httpMethod = new HttpMethodConstraint(HttpMethod.Get)
   });

However, if I try to invoke the method with a GET I get the following response. 但是,如果尝试使用GET调用该方法,则会收到以下响应。

{
Message: "The requested resource does not support http method 'GET'."
}

If I switch the constraint to POST and invoke with a POST, all works well (and the action shows on the help page). 如果我将约束条件切换为POST并通过POST调用,那么所有方法都可以正常工作(并且操作将显示在帮助页面上)。

Any ideas what I'm doing wrong? 有什么想法我做错了吗?

Why not just use conventions and http verbs. 为什么不只使用约定和http动词。

Ex: Define actions called Get, Post, Put etc and just hit them with the root part of the url and use http verbs to tell WEB Api which action to respond with. 例如:定义名为Get,Post,Put等的动作,然后用url的根部分打入它们,并使用http动词告诉WEB Api响应哪个动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM