How to change the URL in the browser URL Bar - c#

I am using server.transfer() method in my asp.net application to redirect the response. But I am running into the problem that it sets the previous page url (from where the original request for page was generated) at the browser url bar. I want to change the url in the browser. is it even possible??
I looked into it and i know that the Request has a url property but its read only. does any one know a way to change the url in the request?

Use Response.Redirect(); instead of server.transfer(); and it redirects in the browser.
If you can't do taht, you could use pushState (at least where it's aviable) to change the URL, but it seems a bit of a overkill...
The best way is clearly to change
server.transfer();
to
Response.Redirect();
EDIT
as you want to have the maximum performance, you could should use Response.Redirect with two parameters, and set the second to true.
so instead of
server.transfer(url);
you should have
to
Response.Redirect(url, true);
That causes the current request to abort and force a instant redirect.

Description
You can't change the Url of the Current request because it is already running.
I think you want to do a redirect.
The Redirect method causes the browser to redirect the client to a different URL.
Sample
Response.Redirect("<theNewUrl>");
Update
If you want to change the Url in the Browsers Address Bar without doing a requestion read this:
Can I change the URL string in the address bar using javascript
More Information
MSDN - Response.Redirect Method

Server.Transfer() is just changeing which content you send back.
Response.Redirect() is what you need to tell the browser to go to a new page
You cannot change the URL of a request - it would make no sense, the URL is what your client (the browser) has asked for.

No, you cannot change the URL in the browser like that. That would be a pretty massive security hole if you were able to do that. http://EvilDomain.com would be able to seamlessly masquerade as http://YourOnlineBank.com and no one would be any the wiser.

Related

Getting the current URL that is shown in the browser

Currently I'm trying to get the current URL that is shown in the browser.
If I use
Request.Path
I get https://this.website.com:443/Default.aspx which is technically correct.
However the URL displayed in the browser itself is https://this.website.com/.
Using any of the Request options still will show Default.aspx.
I need to ultimately detect wether or not the url in the browser is https://this.website.com or http://this.website.com/Default.aspx and then redirect to Default.aspx if it's not there.
Btw complicating things more is the https redirect in my web.config.
You can get it from the request in the httpcontext.
HttpContext.Current.Request.Url
Updated:
If you want to tell wether the current url is / or /default.aspx. You can use the RawUrl property of the request. This field will contain the whole url.
HttpContext.Current.Request.RawUrl

Response.redirect showing the previous page url in the address bar

I used Response.Redirect in my code and it works fine but the url is not correct.It always shows the previous page url.
Here is my code.
Response.Redirect("Main.aspx?DocAddEdit=customer incident");
Please suggest.
I even tried server.transfer, but same thing happened.
I find it highly unlikely that the url is the same. According to the MSDN documentation on Response.Redirect
ASP.NET performs the redirection by returning a 302 HTTP status code.
That means the browser actually performs the redirect and requests the new url.
Server.Transfer, on the other hand, will render and return the url you specify, in which case the url in the user's browser will stay the same.
Are you sure you tried Response.Redirect and not just Server.Transfer?
Recently I came across the same issue with the previous URL remains after Response.Redirect and
I could solve it using Response.End() after the line Response.Redirect().

How to get original google shortened URL after it's been resolved

So I have a google shortened Url, and once I click on it and hit my controller, I want to be able to see what the original goo.gl url was before it got resolved. How on earth do I do this?
I've tried Request.UrlReferrer.AboluteUri and System.Web.HttpContext.Current.Request.Url.AbsoluteUri but neither seem to work. They all simply return the resolved absolute uri. Any help on this would be greatly appreciated.
Here's an example of the shortened URL - http://goo.gl/WSrJ6
This would then take the user (in testing at least) to localhost:81/college/events/details/23
So basically, when I hit the Details Controller, how do I get the original shortened url back?
Ok, this feels like more of a workaround but...
I don't see any way to resolve the shortened url from the goog.gl service. However, you could send a web request to goog.gl that only uses the HEAD HTTP verb using the shortened url.
Then, in the response to the HEAD request, the location header will be the original url (because it will send back a redirect request 301).
You can check out the response by using this tool. Put in your shortened url and then choose the HEAD verb before posting.
https://developers.google.com/url-shortener/v1/getting_started
You do a get request with the following url and you get a Json with the long url
https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/WSrJ6
Any normally configured browser sends the header HTTP_REFERER.
Doesn't a simple Request.UrlReferrer work? Or, something like HttpContext.Current.Request.ServerVariables["HTTP_REFERER"] or ServerVariables["HTTP_REFERER"]?
That, depending on where you are coding, but the point is to grab the header from the request.
HTTP_REFERER should always contain the previous (referer) url. I don't see why you're getting the resolved url there, unless it's a second redirection (e.g. your shortener pointed to http://server.com and your web server is configured to redirect missing www to http://www.server.com).
HTH
Francisco

Is there any way to check Rewrited URL in browser

In my Project i don't want to show query string values to users. For that case i used URL Rewriting in asp.net. So my URL Looks like below.
http://localhost/test/default.aspx?id=1
to
http://localhost/test/general.aspx
The first URL will be rewrites to second URL, but it will still executes the default.aspx page with that query string value. This is working fine.
But my question is that, is there any way the user can find that original URL in browser?
The answer is no.
The browser can't tell what actual script ended up servicing the request - it only knows what it sent to the server (unless the server issued a redirect, but then the browser would make a new request to the redirect target).
Since URL rewriting takes an incoming request and routes it to a different resource, I believe the answer is yes. Somewhere in your web traffic you are requesting http://localhost/test/default.aspx?id=1 and it is being rewritten as the new request http://localhost/test/general.aspx.
While this may hide the original request from displaying in the browser, at some point it did send that original URL as an HTTP GET.
As suggested, use Firebug or Fiddler to sniff the traffic.
I figured answer for my question. We can easily found the rewritten urls. If we saw the view source of that page in browser then we can see that original url with querystring values.

Possible to get return url back from javascript/jquery?

I have a problem that when a user times out on my site they are still logged in. So they can still do an ajax request. If they do an ajax request on my site my asp.net mvc authorization tag will stop this.
The authorization normally then redirects the user back to the signin page if they fail authorization.
Now since this is an ajax request what seems to be happening is it send the entire page back rendered as html. So the user never gets redirect since I just got the entire page send to me as html.
However firebug says this in the console:
http://localhost:3668/Account/signIn?ReturnUrl="return" ( this is not in the actual url bar in the web browser so I can't go up there and get it. I only can seem to see it through firebug.)
So I am not sure but maybe if I could somehow grab this url from inside my errorCallback area that would be great.
Since from my testing no error code is sent back(200 OK is sent). Instead I just get parsing error(hence why errorCallback is called) but I can't assume that every time I get parsing error it means the user timed out.
I need something better. The only other option is too look at the response and look for key works and see if it is the signin page what I don't think is that great of away to do it.
You probably want to do one of two things:
Write your server code such that ajax requests return an ajax error when a session is expired. That way the javascript will expect a return code that indicates a session timeout, and you can tell the user the session expired.
If an elegant solution isn't forthcoming because of how your framework handles this stuff, just put a chunk of HTML comment in your login page like Uth7mee3 or something; then check for the existence of that string in your ajax code.
Alternative, you can also set a timer on the web page that figures out when the session is about to time out and warn the user with a little message that lets them renew their session. Once it times out, blank out the page and give them a link to login again.
How about having a script in the Loginpage
if(document.location.href != "/Account/Login")
{
document.location.href = "/Account/Login"
}
This would work if you try to render partials in an ajax request.
(Not if you expect json)
What is the status code of the response in this situation? I think you should be able to check for a 302 here. If not, the Location header would be the next best way to check for the sign-in page.
This isn't an answer to your specific question, but the way I deal with this is to have a some client-side code that understands about the session length and prompts the user to renew a session just prior to it being ready to expire if they haven't moved off the page. If the user doesn't respond to the prompt in time, it invokes the logout action of the site -- taking the user to the login page.
You can find more information on the exact implementation, including some code, on my blog: http://farm-fresh-code.blogspot.com.

Categories