C#WebAPI查询字符串路由

时间:2019-01-08 16:00:02

标签: c# asp.net-web-api binding routing query-string

下面是我的控制者和动作:

[RoutePrefix("api/PaymentManagementController")]
public class PaymentManagementController : ApiController
{
    [HttpGet]
    [Route("")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}

这是WebApi.config:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/",
    defaults: new { id = RouteParameter.Optional }

此链接有效:

  

localhost:59253 / api / PaymentManagementController?commandType = check&account = ParamValue1&txnId = SomePrefix0123456789

但是,我希望它是:

  

localhost:59253 / api / PaymentManagementController / CheckStatus?commandType = check&account = ParamValue1&txnId = SomePrefix0123456789

即使在将“路由”标记为以下内容之后:

[RoutePrefix("api/PaymentManagementController")]
public class PaymentManagementController : ApiController
{
    [HttpGet]
    [Route("CheckStatus/{commandType}/{account}/{txnId}")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}

我收到以下错误消息:

  

      找不到与请求URI'localhost:59253 / api / PaymentManagementController?commandType = check&account = ParamValue1&txnId = SomePrefix0123456789'相匹配的HTTP资源。       在与请求匹配的控制器“ PaymentManagement”上未找到任何操作。

5 个答案:

答案 0 :(得分:0)

将路线更改为

[HttpGet]
    [Route("CheckStatus")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }

答案 1 :(得分:0)

正如我在上面发布的

[RoutePrefix("api/PaymentManagementController")]
public class PaymentManagementController : ApiController
{
    [HttpGet]
    [Route("CheckStatus")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }

使用测试网址:

  

localhost:59253 / api / PaymentManagementController / CheckStatus?commandType = check&account = ParamValue1&txnId = SomePrefix0123456789

但是您尝试的第二种模式更现代:

[RoutePrefix("api/PaymentManagementController")]
public class PaymentManagementController : ApiController
{
    [HttpGet]
    [Route("CheckStatus/{commandType}/{account}/{txnId}")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}

您应该使用URL进行测试

  

localhost:59253 / api / PaymentManagementController / CheckStatus / check / ParamValue1 / SomePrefix0123456789

答案 2 :(得分:0)

    Try it.
    [HttpGet]
    [Route("CheckStatus")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }

答案 3 :(得分:0)

您可以在控制器上定义属性,并且可以使用属性路由按预期工作。属性路由中定义的网址的工作方式如下:Route Prefix / Route / Parameters,对于您的代码,它也适用于api/PaymentManagementController?commandType=check&account=ParamValue1&txnId=SomePrefix0123456789

要获得要求的工作网址,请尝试以下代码:

[RoutePrefix("api/PaymentManagementController")]
public class PaymentManagementController : ApiController
{
    [HttpGet]
    [Route("CheckStatus")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}

答案 4 :(得分:0)

从RoutePrefix删除“控制器”或删除整个属性-控制器会自动映射。

[RoutePrefix("api/PaymentManagement")]

要使用不带控制器的预期链接:

  

localhost:59253 / api / PaymentManagement / CheckStatus?commandType = check&account = ParamValue1&txnId = SomePrefix0123456789

我会选择:

public class PaymentManagementController : ApiController
{
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}

要与控制器建立预期的链接:

  

localhost:59253 / api / PaymentManagementController / CheckStatus?commandType = check&account = ParamValue1&txnId = SomePrefix0123456789

public class PaymentManagementControllerController : ApiController
{
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}