Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've seen plenty on this regarding url rewrite rules et al, but the requested url doesn't make it to by endpoint.

request:
PUT http://api.host.com/item/{itemcode}

itemcode can be "B0015/A" for example.

I need to parse this as B0015/A not the route B0014/{SomeOtherUrl}.

So I figure that the routing is making things difficult and that I may not be able to get the %2f all the way down into the endpoint (please correct me if i'm wrong here).

Now I'm looking for other options. I don't think that an itemcodes have double slash ("//") so if there is a way I can have a params[] endpoint, that might work:

C#
[Route("item/{itemcode:string[]}"] 
public Task<IHttpActionResult> ThisWouldNeverReallyWork( params string[] p)

XD

any suggestions welcome. I'm quite stuck here atm

What I have tried:

I have already tried playing with URL rewrite:


XML
<rewrite>
  <rules  useOriginalURLEncoding="true">
    <rule name="UrlEncoding" >
      <match url="^(https?://).*" />
      <action type="Rewrite" url="{R:1}{HTTP_HOST}/{URL}" />
    </rule>
  </rules>
</rewrite>


My next move it to try to route the request manually, but that's gonna take a while.

Thanks
Andy
Posted
Updated 1-Jul-19 5:02am

1 solution

why not base64 encode the value, then decode when you receive it? Makes life a lot easier so you don't have to worry about strange chars.

Here's your value encoded : QjAwMTUvQQ==
Try decoding at: Base64 Decode and Encode - Online[^]

C# has libraries that make base64 encode/decode very easy.
 
Share this answer
 
Comments
[no name] 1-Jul-19 11:03am    
Is not Uri.EscapeUriString or UrlEncode exactly to solve this kind of problem?
raddevus 1-Jul-19 11:14am    
I don't believe UrlEncode will work here, since it won't encode the slash (/). if you open your browser tools F12 and type the following in the console you'll see you get the same string (non-encoded): encodeURI("B0015/A") // javascript's version of url encode.
I tried the other one also and it does the same -- does not encode the slash:
Console.WriteLine(Uri.EscapeUriString("B0015/A"));
If you want to try base64 encoding as javascript you can try:
btoa("B0015/A") // in console
[no name] 1-Jul-19 11:18am    
Thanks a lot for this. But anyway this looks promising for me: URL Encode Decode - URL Percent Encoding and Decoding.[^]
Andy Lanng 1-Jul-19 11:38am    
from C# EncodeDataUrl(url) will encode "/" to "%2f"
First issue is that IIS decodes it. Using Url rewrite I have fixed that but then Routing decodes it too.
Double encoding is a pretty poor solution as it would open up all kinds of shenanigan vulnerabilities.
The issue isn't encoding, but how IIS and MVC decode.
Andy Lanng 1-Jul-19 11:41am    
Encoding is easy enough ("/" becomes "%2f"), but keeping it encoded through IIS and MVC Routing isn't :S

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900