简体   繁体   English

Web API 路由无法识别参数

[英]Web API Routing Not Recognising Parameters

I have the following WebAPI controller:我有以下 WebAPI 控制器:

namespace MyApp.WebApi.Controllers
{
    [RoutePrefix("api/listing")]
    public class ListingController : ApiController
    {
        [Route("{firstparam:int?}/{nextparam:int?}")]
        public IEnumerable<ListItem> Get(int firstparam = 100, int nextparam = 12)
        {
             // firstparam is always 100, and nextparam is always 12

However, I've tried specifying the URL:但是,我尝试指定 URL:

http://localhost:56004/#/listing?firstparam=2

If I specify the URL like this:如果我像这样指定 URL:

http://localhost:56004/#/listing/2

Then it breaks the routing.然后它打破了路由。

Clearly I'm missing something regarding routing;显然我遗漏了一些关于路由的东西; please could someone point me in the right direction?请有人能指出我正确的方向吗?

You are using multiple optional parameter which don't work well for routeTemplates.您正在使用多个可选参数,这些参数不适用于 routeTemplates。 Normally the last parameter tends to be the optional parameter.通常最后一个参数往往是可选参数。

Documentation: Attribute Routing in ASP.NET Web API 2: Optional URI Parameters and Default Values文档: ASP.NET Web API 2 中的属性路由:可选的 URI 参数和默认值

FIrst make sure the attribute routing is enabled首先确保启用了属性路由

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

To get what you are after得到你所追求的

[RoutePrefix("api/listing")]
public class ListingController : ApiController {

    //GET api/listing
    //GET api/listing?firstparam=x
    //GET api/listing?nextparam=y
    //GET api/listing?firstparam=x&nextparam=y
    [HttpGet]
    [Route("")]
    public IEnumerable<ListItem> Get(int firstparam = 100, int nextparam = 12) { ... }
}

The problem with having multiple inline parameters that are optional is that the router wont know which to use which is why they tend to be at the end of the url.具有多个可选的内联参数的问题是路由器不知道使用哪个,这就是为什么它们往往位于 url 的末尾。

However to get them inline like how you mentioned in your example you are going to need multiple routes.但是,要像您在示例中提到的那样将它们内联,您将需要多条路线。

[RoutePrefix("api/listing")]
public class ListingController : ApiController {

    //GET api/listing
    [HttpGet]
    [Route("")]
    public IEnumerable<ListItem> Get() { return Get(100, 12);  }

    //GET api/listing/2
    //GET api/listing/2/5
    [HttpGet]
    [Route("{firstparam:int}/{nextparam:int?}")]
    public IEnumerable<ListItem> Get(int firstparam, int nextparam = 12) { ... }
}

You could try using [FromUri] attribute within the params of the Get() method to extract any query params being passed into the "/listing" uri along with a class that consists of int properties firstparam and secondparam.您可以尝试在 Get() 方法的 params 中使用[FromUri]属性来提取任何被传递到“/listing”uri 的查询参数以及一个由 int 属性 firstparam 和 secondparam 组成的类。

namespace MyApp.WebApi.Controllers
{
    [RoutePrefix("api/listing")]
    public class ListingController : ApiController
    {
        [Route("")]
        [HttpGet]
        public IEnumerable<ListItem> Get([FromUri] ClassRepresentingParams params)
        {

Hopefully that helps.希望这有帮助。

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

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