Does Response.RedirectPermanent automatically pass querystring variables? - query-string

I have googled to death this to no avail. I have a page that we no longer want to use, however, dpending on how it was called, I want to pass the querystring along if there is one. I wrote some ugly code to add the querystring if there is one, but if the Response.RedirectPermanent already does it then I could skip that step. The microsoft web site is not clear. Thanks.

A 301 redirect will redirect to whatever you pass into RedirectPermanent, exactly that. It will not re-append the curent query string to what you are redirecting to.

Related

Is it possible to return HTTP code 200, but give a "better" url without using 3xx?

Consider StackOverflow, where each question has a unique ID, but URLs are often overridden to include a stub in the URL. For readability and other reasons the stub helps users know they are at the right place.
I have a site that returns 200 when calling a URL like:
http://stackoverflow.com/questions/28057406/
But want the URL to update to:
http://stackoverflow.com/questions/28057406/is-it-possible-to-return-http-code-200-but-give-a-better-url-without-using-3x
The first call is technically valid and the code can retrieve the object and render it perfectly fine, but I'd like to update the URL to use the stubified one.
I'd prefer to do this without a redirect as just getting the ID causes a database call to get the object. Which would mean with a redirect the process would be:
Call http://stackoverflow.com/questions/28057406/
Retrieve item 25257999 from the database to get the name to make the stub
Redirect to http://stackoverflow.com/questions/28057406/is-it-possible-to-return-http-code-200-but-give-a-better-url-without-using-3x
New HTTP Call, so retrieve item 25257999 from the database to render the final page.
If possible I'd like to not use Javascript either.
So, is it possible to return Location as part of a HTTP header with a status code of 200 and the actual page, or am I stuck using 3xx calls or Javascript?
If you are just doing HTTP, you can either choose to redirect, or not choose to redirect... You can also (with Content-Location) tell the client that the canonical address is actually somewhere else... but no browser will respond to that.
To avoid the database-call, you could of course just cache the result.
If you are in a browser however, you can dynamically update the current address without forcing a refresh, with window.history.pushState.
For more information about that call, see this other SO answer:
Modify the URL without reloading the page

changing url for security reason asp.net

I am writing an asp.net web application for internal automation.So, I am not thinking about SEO or user-friendly URLs. Just I want to change URLs for hide path and file name and query string. For example I want this URL "http://test.com/Admin/Create.aspx?id=345&name=pin" be shown for user something else that is not understandable like an encrypted URL "http://test.com/enc=nidfvegvbervmxvpazxczxcwefve" or show all URLs in all formats in a same way like "http://test.com/".
I read some articles about URL rewriting and URL routing. However, I think their method work reversely. I mean when user write "http://test.com/products/book", these method could assume it as "http://test.com/products.aspx?type=book" but I want that user couldn't see real URL ever.
Any idea?
I think what you might be looking for is Server.Transfer - check out:
http://msdn.microsoft.com/en-us/library/540y83hx.aspx
Just Encrypt the query string and decrypt on calling it
Something like
http://www.aspsnippets.com/Articles/Encrypt-and-Decrypt-QueryString-Parameter-Values-in-ASPNet-using-C-and-VBNet.aspx

Having two ? in a URL - Is it valid?

Here is the thing:
Let's say I have a base URL like this:
www.mysite.com?my_first_param=1&my_second_param=2
But now I want to add a new parameter to my URL:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com
Imagine that I am using a service where they add new tracking parameters to my URL. For example:
first_tracking=value1&second_value=value2
Since my BASE url already has a "?" the parameters are added with a "&", so my final URL would look like this:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com&first_tracking=value1&second_value=value2
This is a correct URL with several parameters, but when I do the redirect to www.myothersite.com, since the parameters start with a "&", they get lost. Would it be correct to add the tracking parameters with a starting "?" ? Like this:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com?first_tracking=value1&second_value=value2
If not, what would be a good approach for dealing with this? I believe is responsibility of the redirect to pass through the tracking parameters to the redirect URL.
You should URLencode each param name and value properly, so if the map of params you want is this:
my_first_param => 1
my_second_param => 2
my_redirect_site => www.myothersite.com?first_tracking=value1&second_value=value2
then you should pass this as the query string:
?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com%3Ffirst_tracking%3Dvalue1%26second_value%3Dvalue2
You should be using a library which does this already to build up URIs to handle this encoding for you.
No, you can't have a second question mark in a URL.
Furthermore, if you have ampersands in the redirect URL, they will be seen as separate parameters for the main URL, and not seen as connected to the redirect URL.
If you want to do a redirect like this, you need to URLEncode the whole of the redirected URL. There are standard functions in most web-facing languages to do this.
Encode the parameter, "?" would be replaced by %3F
It depends on the amount of control you have over the service adding the tracking parameters.
Can you change the url after the parameters have been added?
If you can, then you should use a url builder to add the tracking parameters to your redirect url, then url encode that url entriely, tracking parameters included.
If you are not in control and a third party modifies your url, then you will have to do this when you redirect, read the parameters in the url, take your redirect url and the tracking parameters, add the tracking parameters to the redirect url before redirect.

Nesting HTTP GET parameters (request within a request)

I want to call a JSP with GET parameters within the GET parameter of a parent JSP. The URL for this would be http://server/getMap.jsp?lat=30&lon=-90&name=http://server/getName.jsp?lat1=30&lon1=-90
getName.jsp will return a string that goes in the name parameter of getMap.jsp.
I think the problem here is that &lon1=-90 at the end of the URL will be given to getMap.jsp instead of getName.jsp. Is there a way to distinguish which GET parameter goes to which URL?
One idea I had was to encode the second URL (e.g. = -> %3D and & -> %26) but that didn't work out well. My best idea so far is to allow only one parameter in the second URL, comma-delimited. So I'll have http://server/getMap.jsp?lat=30&lon=-90&name=http://server/getName.jsp?params=30,-90 and leave it up to getName.jsp to parse its variables. This way I leave the & alone.
NOTE - I know I can approach this problem from a completely different angle and avoid nested URLs altogether, but I still wonder (for the sake of knowledge!) if this is possible or if anyone has done it...
This has been done a lot, especially with ad serving technologies and URL redirects
But an encoded URL should just work fine. You need to completely encode it tho. A generator can be found here
So this:
http://server/getMap.jsp?lat=30&lon=-90&name=http://server/getName.jsp?lat1=30&lon1=-90
becomes this: http://server/getMap.jsp?lat=30&lon=-90&name=http%3A%2F%2Fserver%2FgetName.jsp%3Flat1%3D30%26lon1%3D-90
I am sure that jsp has a function for this. Look for "urlencode". Your JSP will see the contents of the GET-Variable "name" as the unencoded string: "http://server/getName.jsp?lat1=30&lon1=-90"

Parameter passing in ASP.Net MVC

i want to make some questions about asp.net mvc.Actually,i am not familiar with web developing.my questions are i have two controllers,one is login and another one is
profile.After login,i want to call profile.so,i use this way,
return RedirectToAction("DiaplayProfile","Profile",new { personID = Person.personID});
my problem is parameter that i passed are shown i URL.i don't want to show.In web developing,if we use post method to send data,parameters are not shown in url,is it correct?for me,i need to pass parameter ,but,i don't want to show at url.
How i use in post method in MVC RedirectToAction?i also tested with TempData
to pass data,but,it's gone after if i make refresh.how i hanlde this?And also,
can i use [AcceptVerbs(HttpVerbs.Post)] for solve this problem?
guide me right ways, please.
Regards
Chong
You can't do a post as part of a redirect; a redirect must be a get request since you're simply passing the browser the URL to which to redirect. In MVC, having the id as part of the URL is actually considered to be a good thing. I would embrace it. Rather than having the parameter be personID, I would use id so that the default routing can be used. Eventually I'd want to end up with a URL that looks like:
http://example.com/profile/display/1345
where 1345 is the id of the person in question. If you use default routing, that would mean that you have a ProfileController with a Display method that takes a single id parameter (of type int). You would redirect to it using:
return RedirectToAction( "display", "profile", new { id = 1345 } );
BTW, you can't use the AcceptVerbs attribute to control how a redirect is made, only to control what HTTP verbs the action will respond to. In this case, I think you really do want to accept a GET and I think you really do want to have the id in the URL.

Resources