How do I pass a URL as Action Parameter? - asp.net-mvc

I have an action that expects a string containing a path to a resource ie "/123/something.html"
I am trying to user HttpUtility.UrlEncode() to encode the parameter but then the slashes are encoded to contain '%' characters and this results in a 400 error from the server. How can work around this in ASP.NET MVC2?

you certainly can pass those characters in a URL but they just have to be passed in the query string in the part after the question mark
Ex : http://localhost/servercrm/contacts/query?search=%25%20%25

Related

Base 64 encoded querystring parameter getting characters replaced

I have a querystring parameter that is an encoded string that gets converted to Base64. That parameter is then embedded in a link within an email. When I click the link in the email, the querystring parameter has had all the + characters within it replaced by space characters. There are no other differences. Is there a method I can call to sanitise the string and effectively replace the spaces with pluses again. I'm currently doing a string replace which is a bit fat hack. Something is causing the replacement but I'm not sure what. Has anyone come across anything like this before?
Example - querystring parameter value within URL of the browser:
yo3rZZbZyG4UCN+L3pcTYJXmWEggnkW1qcyJk2uBrVTtGUSKIlBcJ8e9TSx8BHjHJv0JhI8H6LbIqUl+3lA7qn+lOgpSi3rCGN4bm5moOWcCA449C1Z3zj7J1FkOXH2HMox4VUZ7x7fF65MRwuBBmw==
Value of string within controller action:
yo3rZZbZyG4UCN L3pcTYJXmWEggnkW1qcyJk2uBrVTtGUSKIlBcJ8e9TSx8BHjHJv0JhI8H6LbIqUl 3lA7qn lOgpSi3rCGN4bm5moOWcCA449C1Z3zj7J1FkOXH2HMox4VUZ7x7fF65MRwuBBmw==
You should URL encode the base64 string to the link, so it is:
yo3rZZbZyG4UCN%2BL3pcTYJXmWEggnkW1qcyJk2uBrVTtGUSKIlBcJ8e9TSx8BHjHJv0JhI8H6LbIqUl%2B3lA7qn%2BlOgpSi3rCGN4bm5moOWcCA449C1Z3zj7J1FkOXH2HMox4VUZ7x7fF65MRwuBBmw%3D%3D
HttpUtility.UrlEncode(base64str) in .NET, or encodeURIComponent(base64str) in javascript
you can use System.Web.HttpServerUtility.UrlTokenEncode (from http://brockallen.com/2014/10/17/base64url-encoding/#comments)
It is doing this because the + sign is interpreted as a marker to say that another parameter follows. This is why it is getting mangled. You should URL encode your string before you pass it to the server.

What does the symbol ? mean in a URL?

What does the symbol ? mean in a URL?
That portion of the URL (ie. after the ?) is known as the query string.
http://en.wikipedia.org/wiki/Query_string
It is used to pass parameters into web applications.
For example, in ASP.NET I might have an .aspx page like so:
http://example.com/myapp/default.aspx
Inside my codebehind for that page I can look for the presence of any query string parameters:
string paramValue = Request.QueryString["param"];
So if someone visits my page with the URL of http://example.com/myapp/default.aspx?param=abcd
Then the value of paramValue will be "abcd".
RFC for http protocol, section 3.2.2 http URL
"?" - is delimiter between "absolute path" and "query"

How can I send a GET request containing a colon, to an ASP.NET MVC2 controller?

This works fine:
GET /mvc/Movies/TitleIncludes/Lara%20Croft
When I submit a request that contains a colon, like this:
GET /mvc/Movies/TitleIncludes/Lara%20Croft:%20Tomb
...it generates a 400 error. The error says ASP.NET detected invalid characters in the URL.
If I try url-escaping, the request looks like this:
GET /mvc/Movies/TitleIncludes/Lara%20Croft%3A%20Tomb
...and this also gives me a 400 error.
If I replace the colon with a | :
GET /mvc/Movies/TitleIncludes/Lara%20Croft|%20Tomb
..that was also rejeted as illegal, this time with a 500 error. The message: Illegal characters in path.
URL-escaping that | results in the same error.
I really, really don't want to use a querystring parameter.
related:
Sending URLs/paths to ASP.NET MVC controller actions
I found that URL encoding did not work, but custom encoding did.
I guess ASPNET MVC uses the filesystem to do the parsing and routing, because a character in the URL that is not legal in the filesystem, causes a 500 or 400 error.
So what I did was replace colons with the unicode
¡ character in the javascript side, and then do the converse in the action. like this:
browser:
function myEscape(s){
return s.replace(':', '%C2%A1').trim();
}
in the action, call this conversion before using the argument:
private string MyCustomUnescape(string arg)
{
return arg.Replace("¡", ":");
}
The same approach works for slashes - just pick a different unicode character. Of course if your string arguments themselves are unicode, then you'll have to use non-printable characters for the "encoded" forms.
If SEO is not a problem you may use base64 and then urlencode that. After the first step every character you'll have will be easily encoded. Decoding in .NET is as easy as using the helper in System.Web.HttpUtility and System.Convert.
Similar answered here:
https://stackoverflow.com/a/12037000/134761
Use question mark and ampersands for arguments and URL encode the arguments.
Example: GET /mvc/Movies/TitleIncludes?title=Lara%20Croft%3A%20Tomb
I agree it would be nice to encode things into the url as well, but there is probably a good reason not to.

Bad request 400 for HttpUtility.UrlEncoded URL segments

So, if there are url encoded segments in my apps MVC url, IIS throws a BAD REQUEST 400.
e.g.
http://u.lasoo.com.au/Offer/*9289--750W-Generic-ATX12V-Power-Supply-%252449dot99/6355
<--- the '*' and '%' are causing this error.
Notice that http://u.lasoo.com.au/Offer/The-Giant-Good-As-Gone-7-Day-Sale/6354 works fine.
What's the correct way to convert an arbitrary string into an accepted MVC URL segment?
UPDATE: the URl segment should resemble the original string. Base64 encoding completely transformed the string.
Instead of passing the info in the url you can pass it as a get parameter. Like this:
http://u.lasoo.com.au/Offer/?id=*9289--750W-Generic-ATX12V-Power-Supply-%252449dot99/6355
Have you tried UrlEncode? MSDN
Try a string replace to strip out or substitute symbols ":", "%", "*", "/" - any symbols illegal within a folder name. They seem to screw up everything royally and appear to be a design weakness of the URL routing system.

Pass URL as get parameter?

I'm trying to pass u url as parameter to a get method.
I defined a route that accepts a {*url} parameter so I can send "/" characters without it separating my parameter.
As soon as there is a ":" in the url (like in http: or localhost:3857 for example), the method never gets hit.
The Html.ActionLink method escapes it's parameter itself, but it doesn't seem to escape the ':'. I cannot escape it manually because then the escape characters get escaped by the very same Html.Actionlink method.
any ideas?
Use EncodeUrl before you pass it, and then decode it on the other side.
I ran into the same problem. I ended up removing the Html.ActionLink and replaced it with:
#item.Title
#item.ID is a url returned from the netflix api, example http://api.netflix.com/catalog/titles/series/70021357/seasons/70021357. Now my url looks like this - /Home/Movies?id=http://api.netflix.com/catalog/titles/series/70021357/seasons/70021357, and I just used Request.QueryString to get the value in the controller:
Request.QueryString.Get("id")
Probably not ideal but it works for now.
It's a bit of a hack, but you could replace the ':' with '%3A' (which is the escaped form), and see what the ActionLink does with it. If it's escaped once more, you'd have to replace the twice-escaped version back to ':' at the server, otherwise just replace '%3A' back to ':'

Resources