Click here to Skip to main content
14,921,361 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.
   
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
[no name] 1-Jul-19 11:59am
   
Ok I understand now. But that sounds horrible if it will be manipulated :(
Andy Lanng 1-Jul-19 11:39am
   
Not a bad shout. I'm going to keep that in my back pocket :)
I'm gonna try to crack into the RouteCollection to see if I can invent some middleware that can cope with encoding data in the url first. I'll post my results
raddevus 1-Jul-19 13:13pm
   
Check out the selected answer on this SO: https://stackoverflow.com/questions/6328713/urls-with-slash-in-parameter
Andy Lanng 2-Jul-19 4:23am
   
Usually we use our own internal guid. The issue is around a Sync-Service that isn't aware of our Id so has to use it's own primary key: the itemcode >_<
Ok - I tried several approaches:
Custom Routing: This used to work (was always complex), but in later versions of .Net it has become untenable. It's far too much to introduce to our project.
CatchAll route: "items/{*itemcode}" would be fine but be do have url paths after the itemcode ("items/{itemcode}/bom"). We could change the order of items but we even have the complication of "items/{itemcode}/{bomitemcode}" which breaks that option in our case too.
Base64 encoding: This is the only case that would work for us. Any 'non-url' encoding would suit, but my team didn't like the idea (they're an odd bunch, but I do have to keep them on-board)
The solution we are going with is to not have the itemcode in the url at all. We will use our sync service post (add only), patch (update only) and put (add/update). Frankly I don't like having a "put" that doesn't 'put' a url in, and I don't see the point of having separate httpmethods anyway, but again: Need to keep the guys on-side.

Thanks for everyone's help. Some really good suggestions here that I'm sure someone will find helpful ^_^

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