URL getting Appended when using Response.Redirect - c#

I have a URL say /Registration/GetName.aspx/?language=English
When i click on a Asp.net Button on the same Page and say Response.Redirect("CheckLoginName.aspx");
It gives me a weird URL
/Registration/GetName.aspx/CheckLoginName.aspx
What should i do
Please Help?

You should use "~/" inside your Redirect
So your code will look something like this
Response.Redirect("~/CheckLoginName.aspx");
Hope this helps

You should remove the trailing / before the query string, since it serves no purpose. Your URL should be /Registration/GetName.aspx?language=English. Another option is to have Response.Redirect("../CheckLoginName.aspx"); This should also work.
I think a solution using a relative path is better, since it is location independant. If you move these two files to another URL, there will be no need for code changes.

Related

Similer like request.getContextPath() in Asp.Net

I need to know that,how we can get the current url path (e.g. http://www.mywebsite.com/) in asp.net.
I found that java have the request.getContextPath() to get that.
Even in dot net we have this,
System.Web.HttpContext.Current.Request.UrlReferrer.Host
System.Web.HttpContext.Current.Request.ApplicationPath
Is there any other ways that will gives the same result.
I am also interested to know how I will use the code in aspx page.
cheers.
I believe what you want is the HttpRequest.AppRelativeCurrentExecutionFilePath Property
The property gets the virtual path of the application root and makes it relative by using the tilde (~) notation for the application root (as in "~/page.aspx").
Could context.Request.Url.ToString() do the trick your looking for?
context.Request.Url.ToString()
returns: "http://localhost:52550/myapp/somepage/"
I got this :)
http://<%=System.Web.HttpContext.Current.Request.UrlReferrer.Host+System.Web.HttpContext.Current.Request.ApplicationPath%>/page.aspx
cheers :) :)

How to remove querystring part from Request.UrlReferrer.AbsoluteUri in C#

I want to remove the Querystring part from my Request.UrlReferrer.AbsoluteUri before the Redirect in C#.
For example, if you have got your
Request.UrlReferrer.AbsoluteUri = "http://localhost:8080/english/index_2011.aspx?logout=true"
Now I want to
Response.Redirect(Request.UrlReferrer.AbsoluteUri) without QueryString part (?logout=true")
Please suggest using C#
use Request.UrlReferrer.AbsoluteUri.ToString().Split('?')[0]
This should do the trick for you.
A cleaner way would be
Request.UrlReferrer.GetLeftPart(UriPartial.Path)
Meaning I want everything up to the path. It should return
"http://localhost:8080/english/index_2011.aspx"
Response.Redirect(Request.UrlReferrer.AbsoluteUri.Substring(0,Request.UrlReferrer.AbsoluteUri.IndexOf('?')));
EDIT
In fact, you can actually use:
Response.Redirect(Request.UrlReferrer.AbsolutePath);
Check it out on MSDN.

C# URL Routing Issue

So I have an interesting problem here. I am using URL Routing to mask the URL but I want to take the spaces out.
For example:
/sanjuan/ but in the database it's San Juan.
An error is thrown when I type it in because clearly theres a space in the DB.
I don't want it conjoined in the DB though.
How can I accomplish this. I just need some ideas to look into.
What rob described is called slugging.
Have a look at this:
http://predicatet.blogspot.com/2009/04/improved-c-slug-generator-or-how-to.html
you could replace the space with - when rewriting the url, and change back the - back to space when reading it.

Problems with Request.QueryString. Reads cache instead of URL

I'm trying to retrieve the value of myID from my URL.
I'm testing this using <%=Request.QueryString["hotelid"] %>.
It only works the first time the page is loaded either in a new browser, or if my project has been rebuild.
My URL string is typical: http://my/path/to/site/?hotelid=2.
If I try <%=Request.QueryString %>, I'm also getting other values as well. Values I do not see inthe URL string.
What am I missing here?
Update:
Using <%=Request.RawUrl%>, I get the following results:
/Util/NotFound.aspx?404;http://localhost/en/Tjenester/Hotellguiden-2/Hotel-informasjon/?hotelid=3
I have NO idea what the /Util/NotFound.aspx?404 is or where it comes from.
My URL looks like this:
http://localhost/en/Tjenester/Hotellguiden-2/Hotel-informasjon/?hotelid=2
Update 2:
I'm currently investigating if it is EPiServer CMS that is using some kind of caching.
Update 3:
I have solved it. EPiServer is using EPnCachePolicyTimeout which isset to 1 hour. Setting this to 0 (zero) solved my problem.
Sometimes is really helps just writing aboutthe problem here, talking "aloud" about it and voila :)
You need to turn off caching or add your parameter names to the config attribute httpCacheVaryByParams or overwrite the custom caching key method and make it diff on every querystring parameter.

Response.Redirect using ~ Path

I have a method that where I want to redirect the user back to a login page located at the root of my web application.
I'm using the following code:
Response.Redirect("~/Login.aspx?ReturnPath=" + Request.Url.ToString());
This doesn't work though. My assumption was that ASP.NET would automatically resolve the URL into the correct path. Normally, I would just use
Response.Redirect("../Login.aspx?ReturnPath=" + Request.Url.ToString());
but this code is on a master page, and can be executed from any folder level. How do I get around this issue?
I think you need to drop the "~/" and replace it with just "/", I believe / is the root
STOP RIGHT THERE! :-) unless you want to hardcode your web app so that it can only be installed at the root of a web site.
"~/" is the correct thing to use, but the reason that your original code didn't work as expected is that ResolveUrl (which is used internally by Redirect) tries to first work out if the path you are passing it is an absolute URL (e.g. "**http://server/**foo/bar.htm" as opposed to "foo/bar.htm") - but unfortunately it does this by simply looking for a colon character ':' in the URL you give it. But in this case it finds a colon in the URL you give in the ReturnPath query string value, which fools it - therefore your '~/' doesn't get resolved.
The fix is that you should be URL-encoding the ReturnPath value which escapes the problematic ':' along with any other special characters.
Response.Redirect("~/Login.aspx?ReturnPath=" + Server.UrlEncode(Request.Url.ToString()));
Additionally, I recommend that you (or anyone) never use Uri.ToString - because it gives a human-readable, more "friendly" version of the URL - not a necessarily correct one (it unescapes things). Instead use Uri.AbsoluteUri - like so:
Response.Redirect("~/Login.aspx?ReturnPath=" + Server.UrlEncode(Request.Url.AbsoluteUri));
you can resolve the URL first
Response.Redirect("~/Login.aspx);
and add the parameters after it got resolved.
What about using
Response.Redirect(String.Format("http://{0}/Login.aspx?ReturnPath={1}", Request.ServerVariables["SERVER_NAME"], Request.Url.ToString()));

Categories