I know you can apply a wildcard in the route attribute to allow /
such as date input for example:
[Route("orders/{*orderdate}")]
The problem with wildcard is only applicable to the last paramter in URI. How do I solve the issue if want to have the following URI:
[Route("orders/{orderdate}/customers")]
Update:
I know there are few options to solve the issue by refactoring the code so please do not offer a solution something like:
[Route("orders/customers/{orderdate}")]
"dd-mm-yyyy"
) The addition of a slash at the end of a URL instructs the web server to search for a directory. This speeds the web page loading because the server will retrieve the content of the web page without wasting time searching for the file.
A trailing slash is a forward slash placed at the end of a URL. It's usually used to indicate a directory (as opposed to a file), but in SEO it can affect your rankings. Take a look at the URLs below and guess which one is 'correct'. Note that one of them has a trailing slash at the end.
@bet.. I think the genericUriParserOptions
is no longer applicable to .net 4.5 or later..
Also as suggested by @JotaBe, you might need to correctly decode the url request. In most case the %2F
will be automatically translated to a slash '/'
. So if you need to escape it you will need to decode the '%'
char in the first place.. so your URL: will look something like: www.domain.com/api/orders/23%252F06%252F2015/customers
Notice the characters '%252F'
will be translated to the actual '%2F'
EDIT
Ok here is the complete solution (Tried it and working for me):
Assuming you have an API endpoint like so:
[Route("orders/{date}/customers")] public HttpResponseMessage Get(string date) { }
In the web.config you will need to set the requestPathInvalidCharacters
to empty which tells the asp.net to allow all request
<system.web> <httpRuntime targetFramework="4.5" requestPathInvalidCharacters=""/> </system.web> <system.webServer> <security> <requestFiltering allowDoubleEscaping="true" /> </security> </system.webServer>
When the client sending the request to the API you will need to make sure to escape the '%'
like so:
www.domain.com/api/orders/23%252F06%252F2015/customers
You then need to decode the request
[Route("orders/{date}/customers")] public HttpResponseMessage Get(string date) { DateTime actualDate = DateTime.Parse(System.Net.WebUtility.UrlDecode(date)); // date is 23/06/2015 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With