Shadarath
Shadarath

Reputation: 187

WebAPI routing cuts wildcard parameter on a question mark

I have a route in controller that should match everything in part of url and put it into string parameter.

What I have is:

    [Route("api/proxy/{proxyId}/{*parameter}")]
    public Task<HttpResponseMessage> Mediate(int proxyId, string parameter)

and for an unknown url, for example:

http://localhost/api/proxy/1/test?a=1&b=2

I would like "parameter" variable to contain:

test?a=1&b=2

Instead, it contains:

test

How can I specify route to not cut everything after question mark? For this particular case I can extract it from Request.RequestUri object, but it would be.. inelegant.

Upvotes: 1

Views: 3741

Answers (1)

JotaBe
JotaBe

Reputation: 39025

You cannot do that. By definition the URL segments doesn't include the query string.

However, you can do something really easy: inside your WebApi controller you have the Request property which contains the Query String:

Request.RequestUri.Query

You simply have to concatenate the url param with this to have what you need. This includes the leading question mark:

The Query property contains any query information included in the URI. Query information is separated from the path information by a question mark (?) and continues to the end of the URI. The query information returned includes the leading question mark.

from Uri.Query Property

If you still want to force it to work in a different way, you'd need to include your own custom route provider, implementeing your own IDirectRouteProvider and registering it. See this: get a list of attribute route templates asp.net webapi 2.2

But doing something like this is unnatural. Why do things exactly in a different way as the standard way that all other people aunderstand and use?

Upvotes: 4

Related Questions