URL Encode string for Href ASP.NET MVC / Razor - asp.net-mvc

I'm trying to build an Href using Razor
The string is going to end up looking like this:
https://www.notmysite/controller/action?order_ID=xxxxxxx&hashComparator=iFxp3%2BszAMaEB%2FnHCtRr9Ulhv0DumtyDumCik4gKypJqi0BdOGXXsr9QDkfefAsLaR1Xy%2BrX9VcuzP1pF2k6dL%2F92UxphzTKqNAZP2SSZGWtvyO5az%2F9JvDY%2Bsq5TBQo7%2FYAAMIU4QxiSX1SBKk8SUNECW3ZmKM%3D
In my model I have the order id and the hash string
As the route is not a part of my site I don't believe I can use the default methods like #Url.Action
and therefore can't use protocol: Request.Url.Scheme
like I've used elsewhere.
So at present I'm trying to figure out how to create this using string functions
I've tried
Url.Encode
Url.EscapeDataString
Html.Encode
but am getting no where fast:
Click Here to be transferred
The output text always has plusses and equals in them and doesn't work.
Which combination do I need?!

I've figured out a way of doing it:
#{
var url = string.Format(
"https://www.notmysite.co.uk/controller/action?order_ID={0}&hashComparator={1}",
#Uri.EscapeDataString(Model.bookingNumber.ToString()),
#Uri.EscapeDataString(Model.hashCode));
}
<p>Click Here to be transferred</p>
Edit 2015 - As mentioned by Jerads post - The solution is to only encode the query string elements and not the whole URL - which is what the above does.

This was the first link that came up for this issue for me. The answers didn't work for me though because I am using core, I think. So wanted to add this in.
System.Net.WebUtility.UrlEncode(MyVariableName)
If Url.Encode doesn't work try the above. Also as stated before don't use this on the entire URL string, just use it for the individual querystring variables. Otherwise there is a good chance your URL wont work.

The problem is that you're trying to encode the whole URL. The only pieces you want to encode are the querystring values, and you can just use Url.Encode() for this.
You don't want to encode the address, the querystring params, or the ? and & delimiters, otherwise you'll end up with an address the browser can't parse.
Ultimately, it would look something like this:
Click Here to be transferred

The easier method is to use #Html.Raw(Model.SomethingUrl)

Related

Coldfusion - Setting/Using URL Parameters

In ColdFusion, I understand how to work with URLs using the query string functions: i.e., the second parameter in:
test.cfm?par1=val1&par2=val2&par3=val3
can be accessed by:
<cfset Param2 = ListGetAt(CGI.QUERY_STRING,2,"&")>
However, I was tasked with making dynamic URLs with the parameters separated simply by slashes, i.e.:
test.cfm/val1/val2/val3
How can I construct a URL this way, and then utilize those parameters in the webpage it links to?
Edit: I understand it would be quite easy to construct a string that is "test.cfm/#val1#/#val2#/#val#" and use it as my URL; I was wondering if there was a cleaner, built-in way for CF to help me do it. I am still lost on how to access those in the page.
Param2 = listChangeDelims(CGI.QUERY_STRING,"/","&");
That will turn "foo=bar&sna=fu" into "foo=bar/sna=fu".
Or listChangeDelims(CGI.QUERY_STRING,"/","&=");
will change it to "foo/bar/sna/fu"

Need some help regarding .net mvc URL routing/rewriting

URL is something like
/home/rawstring13245/rawstring534533453
I want the rule that saves only 13245 to parameter and 534533454 to another but ignore raw strings before them.
how to achieve it in route.config file?
i want this because strings are not parameters , I need only parameters out of string
like:
url:"{controller}/rawstring{action}/rawstring{id}",
what to enter in place of raw string? I don't need those strings. and yeah each raw string is of same length= 10
You can get complete variables as strings and get your desired part by using the sub-string method. Its the quickest solution, i thought it solve your problem.

URL routing issue when data have special symbol

I have developed a ASP.NET MVC application. I have a conroller with the name EmployeeController and it got a method called GetEmployeeByName. GetEmployeeByName() takes a name of type string as parameter.
So When I send a request like this, i get the data back :
someDomain:9999/Employee/GetEmployeeByName/Roger Federer
But if the name contains an '&' (you & me), I get a '400 Bad Request' as response from server.
someDomain:9999/Employee/GetEmployeeByName/you%20&%20me
Even if i encode it dont get a reposne back
someDomain:9999/Employee/GetEmployeeByName/you%20&%20me
What is the right way to encode such (data with special character) data?
What is the right way to encode such (data with special character) data?
The right way is to use a query string parameter and not be putting those things as part of the uri portion. Read the following blog post from Scott Hansleman. I will only quote hos conclusion:
After ALL this effort to get crazy stuff in the Request Path, it's
worth mentioning that simply keeping the values as a part of the Query
String (remember WAY back at the beginning of this post?) is easier,
cleaner, more flexible, and more secure.
As you can see in the blog post there are some hacky ways to make it work and circumvent IIS handling but it simply is not something that I would recommend you venturing into. Just put this name in the query string.

Extend Url Route to apply Url Encoding for each parameter

I am facing a problem that one of my fields need to be shown in the url contains special character (/, \, :).
The stupid way to handle this generate action links by using UrlEncode(). Then UrlDecode is used before consuming in controller. But I think it really stupid because too many places need to be adapted.
So, my problem is there any way to extend the url route or just write my own one to achieve it?
Thanks,
Mike
You can extend the System.Web.Routing.Route object to create a custom route and override the GetRouteData and GetVirtualPath methods. These are called to resolve a route's values and create a URL from given route values, respectively. However, I don't think URLs can contain URL encoded values for / (%2f) within the path portion of a URL though it is ok in a query string.

international characters in url, mvc filtering issue

I have a problem with filtering in asp.net mvc.
I have a page with listed collection and the filter.
Filter has values for filtering collection, it is: category, status and the containing string. The problem is in internationalisation-application can be on more than one language, so the containing string can be special character, for example: ΓΌ
My route is List/{category}/{status}/{containingString}
should I use get method for sending also the containingString or should I use post method.
I am using little trick now, my form is posting to another action method of same controller, this controller get category and status from url and containingString from Request.Form and then redirect to List action method, containingString I am putting in TempData...as you see it is a some kind of dirty hack and I don't like it myself...
Anyone for a better solution to this problem?
Thanks
p.s. stackoverflow rocks!
You could strip the diacritics in the Router?

Resources