Asp.net web api 如何调用WebApi,使用AttributeRouting为每个操作方法实现,从我的客户端调用它并在url中传递查询参数?
我已经为我的Webapi中的每个操作方法实现了属性路由。 行动方法示例如下:-Asp.net web api 如何调用WebApi,使用AttributeRouting为每个操作方法实现,从我的客户端调用它并在url中传递查询参数?,asp.net-web-api,Asp.net Web Api,我已经为我的Webapi中的每个操作方法实现了属性路由。 行动方法示例如下:- [Route("api/DocumentApi/DeleteDocument/{fileInfoId}/{customerAccountName}")] [HttpDelete] public HttpResponseMessage DeleteDocument(int fileInfoId, string customerAccountName) { /
[Route("api/DocumentApi/DeleteDocument/{fileInfoId}/{customerAccountName}")]
[HttpDelete]
public HttpResponseMessage DeleteDocument(int fileInfoId, string customerAccountName)
{
//***
//*** Some body contents
//***
}
现在,我想从客户端示例(Fiddler Web debugger)或浏览器调用上述操作方法,并希望以以下模式传递Url请求:-
http://{localhost:9791}/api/DocumentApi/DeleteDocument?fileInfoId=12&customerAccountName="Manish"
目前,我无法通过上述指定的url请求点击上述操作方法。
但如果我使用如下url模式:-
http://{localhost:9791}/api/DocumentApi/DeleteDocument/12/Manish
我可以点击上面的操作方法。但是对于我的项目需求,我只需要使用带有查询参数的Url。
请建议我的方法,如何实现这一点?
任何回复都将不胜感激。Web API中的路由模板不支持指定查询字符串参数。对于您的场景,不要将
fileInfoId
和customerAccountName
定义为路由模板的一部分,因为这样做会使Web API在您的请求url中严格查找5个段(路由模板中/
字符之间的文本),所以只需将路由模板修改为[route](“api/DocumentApi/DeleteDocument”)]
并保持操作参数不变…使用如下代码:
[Route("api/DocumentApi/DeleteDocument/{fileInfoId}/{customerAccountName}")]
public HttpResponseMessage TestAction(string fileInfoId,string customerAccountName)
{
我很感激您的回答,但我的问题是,如果我有两个同名的操作方法,例如:-如果我在同一控制器中有两个方法,它们具有相同的路由模板[Route(“api/DocumentApi/DeleteDocument”)]和[Route(“api/DocumentApi/DeleteDocument”)],以及如果两个Web Api操作方法采用不同的输入参数。那么,当从客户端(例如浏览器或Fiddler Web调试器)调用时,Web Api如何将两个操作方法识别为不同的方法。谢谢。Web Api确实具有处理此类场景的操作重载逻辑……您看到了什么不同的情况吗现在我的问题几乎解决了。首先感谢你们的合作。如果我遇到任何与之相关的问题,我们会保持联系。我真的学到了很多。