如何调用WebApi,为每个操作方法实现AttributeRouting,从我的客户端调用它并在url中传递Query参



我已经为我的Webapi中的每个动作方法实现了属性路由。动作方法的例子是:-

[Route("api/DocumentApi/DeleteDocument/{fileInfoId}/{customerAccountName}")]
      [HttpDelete]
      public HttpResponseMessage DeleteDocument(int fileInfoId, string customerAccountName)
      {
            //***
            //*** Some body contents
            //***
      }

现在我想从客户端示例(Fiddler Web调试器)或浏览器调用上述操作方法,并希望以以下模式传递Url请求:-

http://{localhost:9791}/api/DocumentApi/DeleteDocument?fileInfoId=12&customerAccountName="Manish"

目前,我无法通过上述指定的url请求命中上述操作方法。但是如果我使用如下url模式:-

http://{localhost:9791}/api/DocumentApi/DeleteDocument/12/Manish

我可以点击上面的动作方法。但是对于我的项目需求,我只需要使用带有查询参数的Url。请建议我的方法,如何做到这一点?

Web API中的路由模板不支持指定查询字符串参数。对于您的场景,不要将fileInfoIdcustomerAccountName定义为路由模板的一部分,因为这样做会使Web API在请求url中严格查找5段(路由模板中/字符之间的文本)…所以只要修改你的路由模板为[Route("api/DocumentApi/DeleteDocument")],并保持动作的参数不变…

使用如下代码:

[Route("api/DocumentApi/DeleteDocument/{fileInfoId}/{customerAccountName}")]      
public HttpResponseMessage TestAction(string fileInfoId,string customerAccountName)
{

最新更新