MVC3 Stripping Query String from my Parameter - asp.net

I have an MVC3 Action that takes a parameter (a URL) that may have a query string in it. My action signature looks like this:
GetUrl(string url)
I expect to be able to send it urls, and it works every time unless there is a query string in the url. For example, if I navigate to:
MyController/GetUrl/www.google.com
the url parameter comes accross as "www.google.com" -Perfect. However, if I send
MyController/GetUrl/www.google.com/?id=3
the url parameter comes accross as "www.google.com/" How do I get MVC3 to give me the whole URL in that parameter? -Including the query string?

It's simple enough to just URL.Encode the passed in URL on the page but you're opening your self to some possible security problems.
I would suggest you encrypt the url then encode it then pass that as your value, the protects you from having people just passing in anything into your app.

That's because system considers id=3 as its own query string. When you construct the link in the view, you need to use #Url.Encode to convert raw url string to encoded string to be accepted as parameter of the controller.

Related

Querystring in C# web application

I have created web application.I am giving one of web page to client as api.Client can pass parameter to web page like below
Ex: www.domainname.com/Testpage.aspx?name=pinky&city=pune&number=xxxxxxxx
In same page Testpage.aspx,I am accessing/fetching querystring like below.
string s= Request.Querystring["name"];
I am not sure how client can call api.I mean to say from browser or code throught.Whether client use urlencode or not?
from code
www.domainname.com/Testpage.aspx?name=Server.UrlEncode("pinky")&city=Server.UrlEncode("pune")&number=Server.UrlEncode("xxxxxxxx")
will below code work ? or does i need to decode?If client did not use Encode then decode work fine?.I want user querystring value further processing and insert into table.
string s= Request.Querystring["name"];
You need not decode it. If they have entered special characters and not encoded then it will not reach your server-side code at all because it will throw a bad request error. If they have encoded at their end then it will be automatically decoded at your end.
Even If they have not encoded, Your decode will work fine.

URL parameters and backbone routing

Backbone.js maintains routing information in a URL after the hash mark, e.g.:
http://localhost:3000#page/hardware/table/?action=details&actionTargetId=5&actionTargetName=10.3.177.185&actionTarget=host
Even though the routing information is in the format ?p1=v1&p2=v2&p3=v3, this portion is not technically part of the url query string since it comes after the hash mark.
My question is if I add an actual query string to our app's urls like this:
http://localhost:3000?newparam=newvalue#page/hardware/table/?action=details&actionTargetId=5&actionTargetName=10.3.177.185&actionTarget=host
is there any possibility of the "newparam" url parameter interfering with the backbone portion?
the problem is your not actually creating a legit query string. your mixing your route with your parameters.
your example is formatted as:
domain ? param # route ? other params
as soon as a questionmark appears in a url everything after it is interpreted as a query string. (in this case) even your route.
personally i suggest using the html5 pushstate.
Backbone.history.start({pushState: true})
this will give you clean(er) urls
http://localhost:3000/page/hardware/table/?newparam=newvalue&action=details&actionTargetId=5&actionTargetName=10.3.177.185&actionTarget=host
that will help your routes to not interfere with your parameters.

How to get # value in query string from a URL coming from an API, not the current request

I get a returned url from using facebook api:
http://www.example.com/#access_token=BAAGgUj7asdasdasdasda4z3cBAFD5ZAyTOMIxtBpjIHsNwLfZC6L9gZAIdSIt3bKP96rg7yAlplMBDA9ZCndAKS9a7m4oRmRmJAxSdCueefweWJrlq3vQv3XaGqTOLofEMjJIVNCYZD&expires_in=0
But i am not sure how to get the token value? As its not in query string or Request.url
Any help?
You can't, assuming this is passed in on the current request, as anything after the # is never sent to the server.
You can capture it in JavaScript and use AJAX to send it to the server, but this will be on a different request.
If you mean you have this URL not from the current request, you can use the Uri class to parse a full URL and get the fragment:
var fragment = new Uri(theUri).Fragment;
var token = fragment.Split(new [] {'&','='}, StringSplitOptions.None)[1];
You can use HttpUtility.ParseQueryString to parse the URL then you can get the value by name e.g.
NameValueCollection query = HttpUtility.ParseQueryString(querystring);
string token = query["token"];
Check this Retrieving Anchor Link In URL for ASP.Net
you cannot get it from the server side, you will need to take if from client side first then pass it to the server in a hidden field or something similar
You have to look for "%23", because "#" is url-encoded to "%23"
reference:
http://www.w3schools.com/tags/ref_urlencode.asp
But you have to know also, that anchors are not available on server side!

what is the difference between rawurl and query string?

i read about httphandler and they use rawurl and then they said :
For example, suppose you rewrote the HTTP handler that processes image requests so that
it is based on the query string instead of the file name
When you make a url rewrite the Request.RawUrl is shown the url that user see on the bar, there you do not have the query strings that you have rewrite.
For example if you rewrite the www.site.com/2/product to www.site.com/product.aspx?id=2 to been able to read the id you need to use the Query string, because the RawUrl did not have it.
HttpRequest.RawUrl Property Gets the raw URL of the current request.
see more http://msdn.microsoft.com/en-us/library/system.web.httprequest.rawurl(v=vs.110).aspx
html addresses use QueryString property to pass values between pages
http://www.localhost.com/Webform2.aspx?name=Atilla&lastName=Ozgur
Webform2.aspx this is the page your browser will go.
name=Atilla you send a name variable which is set to Atilla
lastName=Ozgur you send a lastName variable which is set to Ozgur

Raw, unprocessed URL with ASP.NET Routing

I'm using ASP.NET UrlRoutingModule directly (not through MVC) to map certain routes to their handlers:
RouteTable.Routes.Add(new Route("products/{name}", handler));
Then, at request time, I'm getting the values from each route:
RouteData routeData = HttpContext.Current.Request.RequestContext.RouteData;
routeData.Values.TryGetValue("name", out value);
Everything fine so far, I'm getting the proper values for each route. My problem is encoding: I want to get the raw value of a route data. Example: for the route above, if the requested URL is http://example.com/products/word%2Dword the resulted "name" is "word-word". What I want though is the exact value "word%2Dword".
I know that with ASP.NET I can get the raw unprocessed URL using Request.ServerVariables["HTTP_URL"] but unfortunately I cannot use this here.
Any help would be appreciated.
Thanks
EDIT
My specific problem is that I would like to get more products in a single request using their names. I have for example the following product names: "student,pupil" and "sick,ill" (their name contains a comma). I'm also using a comma to separate names in the request.
I handle the encoding on the client side so the GET request looks like this: http://example.com/products/student%2Cpupil,sick%2Cill (I'm encoding each name separately but I'm not encoding the separator).
On the server side the "name" parameter will be automatically decoded by ASP.NET and the result is: "student,pupil,sick,ill" so now I don't know which is the separator. Request.ServerVariables["HTTP_URL"] returns the URL as I want it ("products/student%2Cpupil,sick%2Cill") so I suppose there has to be a way to get the raw value as a route data.
The "raw value" you're seeing isn't actually the original value, it's what was encoded to make the URL safe for the HTTP Protocol.
http://example.com/products/word%2Dword for instance started out as http://example.com/products/word-word and is turned back into word-word as it comes out of the HTTP transport layer.
If you pass it through Server.URLEncode you will get back the same encoded value (%2D instead of -) - but if you can't use Server variables, are you going to have access to the Server object?

Resources