简体   繁体   English

Web Api属性路由,带有可选参数和查询参数

[英]Web Api Attribute Routing with optional parameters and query parameters

I have this web api controller : 我有这个web api控制器:

[RoutePrefix("api/product")]
public class ProductController : ApiController
{
    [HttpGet, Route("{id?}")]
    public async Task<HttpResponseMessage> GetProduct([FromUri] string param1 = null, [FromUri] string param2 = null, Guid? id = null)
    {
        ...     
    }
}

I would like to access by those uri : 我想访问那些uri:
api/product/?param1=something&param2=smt API /产品/?参数1 =东西&参数2 = SMT
or 要么
api/product/7b55fcee-21e7-4b10-80e3-42b4d9cf913d?param1=something&param2=smt API /产品/ 7b55fcee-21e7-4b10-80e3-42b4d9cf913d?参数1 =东西&参数2 = SMT

However, the first uri does not work, default value for route parameter is not set. 但是,第一个uri不起作用,未设置route参数的默认值。
This uri works : 这个uri有效:
api/product/null?param1=something&param2=smt API /产品/空?参数1 =东西&参数2 = SMT
not this one : 不是这个:
api/product/?param1=something&param2=smt API /产品/?参数1 =东西&参数2 = SMT

I tried to use the type "string" for the route parameter but it still didn't work. 我试图使用类型“字符串”作为路由参数,但它仍然无法正常工作。

Is it the order of parameters ? 是参数的顺序吗?
Or did I misunderstood something about Web Api Route Mapping ? 或者我误解了Web Api Route Mapping的一些内容?

EDIT : My WebApiConfig.cs does not contains default routes : 编辑:我的WebApiConfig.cs不包含默认路由:

public static class EdmWebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
       if (config == null) throw new ArgumentNullException(nameof(config));

       // Attribute routing
       config.MapHttpAttributeRoutes();

       ...
       some declaration of formatters
       ...
    }
}

Should I declare a default route ? 我应该申报默认路线吗?
In my opinion, since I use Attribute mapping, I don't need to do that. 在我看来,由于我使用属性映射,我不需要这样做。

I tried to change my routing like that : 我尝试改变我的路由:

[RoutePrefix("api")]
public class ProductController : ApiController
{

    [HttpGet, Route("product/get/{id?}")]
    public async Task<HttpResponseMessage> GetProduct([FromUri] Guid param1, [FromUri] string param2, Guid? id = null)
    {
      ...
    }
}

This uri api/product/get/?param1=something&param2=smt still does not work. 这个uri api / product / get /?param1 = something&param2 = smt仍然不起作用。
(I tried api/product/?param1=something&param2=smt with the associated routing, not working) (我试过api / product /?param1 = something&param2 = smt和相关的路由,不工作)

Do I need to declare default route in my WebApiConfig.cs ? 我是否需要在WebApiConfig.cs中声明默认路由?
What you can do in WebApiConfig.cs, you should be able to do it in attribute routing, isn't it ? 你可以在WebApiConfig.cs中做什么,你应该能够在属性路由中做到这一点,不是吗? Like optional parameters, constrainsts,... 像可选参数,约束,......

Your route prefix is too specific. 您的路由前缀太具体了。 I believe you want: 我相信你想:

[RoutePrefix("api")]
public class ProductController : ApiController
{
    [HttpGet, Route("product/{id?}")]
    public async Task<HttpResponseMessage> GetProduct([FromUri] string param1 = null, [FromUri] string param2 = null, Guid? id = null)
    {
        ...     
    }
}

Which should work well for you. 哪个应该适合你。 A route prefix is not a route in-and-of itself, so the default route should be less specific. 路由前缀本身不是路由,因此默认路由应该不太具体。

I can't reconstruct this error but I'd like to recommend a better pattern for you to follow. 我无法重建此错误,但我想为您推荐更好的模式。

You have a single method which is too general and needs major examination of the parameters prior to do the actual job. 您有一个过于笼统的方法,需要在完成实际工作之前对参数进行重大检查。

That would be nicer if you to have two separate endpoints. 如果你有两个独立的端点,这将更好。 A general Get method like: 一般的Get方法,如:

Get([FromUri] string keyword = null)

And another one such as: 另一个如:

GetById(Guid id)

The way you have set your RoutePrefix is correct and you don't need to change it. 您设置RoutePrefix是正确的,您无需更改它。

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

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