asp.net web api 2 如何接受 POST 中的查询字符串?
asp.net web api 2 How can I accept a query string in POST?
我有一个控制器,我试图在其中 post 使用查询字符串
http://localhost:53546/api/v1/projects?id=ABA28A61-8898-4739-8464-386C7890E435
但这不会影响控制器 POST
方法
然而这确实
http://localhost:53546/api/v1/projects/ABA28A61-8898-4739-8464-386C7890E435
如何在 post 中接受查询字符串?我不断收到 504 错误:不支持的方法。在 asp.net web api 中是否有一些特殊的语法来接受查询字符串?
这是我的控制器
[RoutePrefix("api/v1/projects")]
public class ProjectController : ApiController
{
private RestClient client;
public ProjectController() {
client = new RestClient("http://localhost:53546");
}
[HttpPost]
[Route("{id:guid}")]
public string Post(Guid id)
{
return "Here is your Post id - " + id.ToString();
}
}
这是我的DoPost
方法
public JToken DoRequest(string path, string method, params string[] parameters)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var fullUrl = url + path + ToQueryString(parameters);
if (DebugUrls) Console.WriteLine("Requesting: {0}", fullUrl);
var request = WebRequest.Create(new Uri(fullUrl));
request.Method = method;
request.ContentType = "application/json";
request.ContentLength = 0;
var response = request.GetResponseAsync().Result;
using (var responseStream = response.GetResponseStream())
{
return ReadResponse(responseStream);
}
}
如果重要的话,这是我的路由配置
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
查询字符串不是路由的一部分。
因此,您需要添加 [Route("")]
以添加一条不向控制器前缀添加任何内容的额外路由。
如前所述,您已将 id 定义为默认路由的一部分。这意味着当 API 控制器有一个方法接受 id 作为参数时,路由引擎将期望在 uri 中而不是从查询字符串中找到它。
在上面的 POST 示例中,如果您在 DoRequest 代码中将参数名称从 id 更改为 postId,并将 id 更改为 postId,那么它应该可以工作。
另一种选择是从您的 routeTemplate 中删除 {id} 并同时删除 id 的默认值。
我有一个控制器,我试图在其中 post 使用查询字符串
http://localhost:53546/api/v1/projects?id=ABA28A61-8898-4739-8464-386C7890E435
但这不会影响控制器 POST
方法
然而这确实
http://localhost:53546/api/v1/projects/ABA28A61-8898-4739-8464-386C7890E435
如何在 post 中接受查询字符串?我不断收到 504 错误:不支持的方法。在 asp.net web api 中是否有一些特殊的语法来接受查询字符串?
这是我的控制器
[RoutePrefix("api/v1/projects")]
public class ProjectController : ApiController
{
private RestClient client;
public ProjectController() {
client = new RestClient("http://localhost:53546");
}
[HttpPost]
[Route("{id:guid}")]
public string Post(Guid id)
{
return "Here is your Post id - " + id.ToString();
}
}
这是我的DoPost
方法
public JToken DoRequest(string path, string method, params string[] parameters)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var fullUrl = url + path + ToQueryString(parameters);
if (DebugUrls) Console.WriteLine("Requesting: {0}", fullUrl);
var request = WebRequest.Create(new Uri(fullUrl));
request.Method = method;
request.ContentType = "application/json";
request.ContentLength = 0;
var response = request.GetResponseAsync().Result;
using (var responseStream = response.GetResponseStream())
{
return ReadResponse(responseStream);
}
}
如果重要的话,这是我的路由配置
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
查询字符串不是路由的一部分。
因此,您需要添加 [Route("")]
以添加一条不向控制器前缀添加任何内容的额外路由。
如前所述,您已将 id 定义为默认路由的一部分。这意味着当 API 控制器有一个方法接受 id 作为参数时,路由引擎将期望在 uri 中而不是从查询字符串中找到它。
在上面的 POST 示例中,如果您在 DoRequest 代码中将参数名称从 id 更改为 postId,并将 id 更改为 postId,那么它应该可以工作。
另一种选择是从您的 routeTemplate 中删除 {id} 并同时删除 id 的默认值。