Question

I have ASP.NET Web API 2.1 project with attribute routing enabled, and a controller action decorated as following:

[Route("api/product/barcode/{barcodeType}/{barcode}")]
public async Task<IHttpActionResult> GetProduct([FromUri] BarcodeSearchCriteria searchCriteria)

where BarcodeSearchCriteria is a complex type:

public class BarcodeSearchCriteria
{
    public string Barcode { get; set; }

    public string BarcodeType { get; set; }
}

It works well for a 'regular' url like this:

/api/product/barcode/EAN/0747599330971

but how in the same time support an url like this:

/api/product/barcode/?barcodeType=EAN&barcode=0747599330971

I used to use it in my *.webtest before switched to 'readable` mode.

Était-ce utile?

La solution

you could have 2 routes in this case:

[Route("api/product/barcode")] //expects values from query string
[Route("api/product/barcode/{barcodeType}/{barcode}")] //expects value from route
public async Task<IHttpActionResult> GetProduct([FromUri] BarcodeSearchCriteria searchCriteria)

Autres conseils

It looks like there is no route defined for the regular Url with query string parameters.

Try making the route parameters optional like this.

[Route("api/product/barcode/{barcodeType=""}/{barcode=""}")]
public async Task<IHttpActionResult> GetProduct([FromUri] BarcodeSearchCriteria searchCriteria)

So it should also match the route template api/product/barcode route.

Haven't tested, though hope you got my point.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top