How to remove querystring part from Request.UrlReferrer.AbsoluteUri in C# - 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.

Related

URL getting Appended when using Response.Redirect

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.

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 :) :)

Passing parameter via quertystring

I want to pass 3 parameters to ashx file to handle the image, for that i used ImageUrl=ImageHandler.ashx?uid=1&iid=1&pid=1 but the image get not bound..
Help with correct syntax..
You may forget " for the code plus the correct location:
ImageUrl="~/ProjectLocationPathToTheImage/ImageHandler.ashx?uid=1&iid=1&pid=1"
If ImageUrl=ImageHandler.ashx?uid=1&iid=1&pid=1 is a part of your URL, then it appears that ImageUrl=ImageHandler.ashx is a query string parameter. If so, you need to change ?uid= to &uid=. What is the full URL that you are having issues with?

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.

C# method to do URL encoding?

what c# class method can I use to URL encode a URL string?
In my use case I want to pass a URL string as a URL parameter itself. So like burying a URL within a URL. Without some encoding the "&" and "?" characters in the inner URL can get picked up when the parameters for the outer Url parameters are processed
thanks
HttpUtility.UrlEncode
System.Net.WebUtility.UrlEncode(str)
HttpServerUtility.UrlEncode might be exactly what you are looking for.
Use Server.UrlEncode
You need an instance of the HttpServerUtility class, because the UrlEncode method is not static.
See http://msdn.microsoft.com/en-us/library/system.web.httpserverutility(v=VS.90).aspx
I am operating with .net framework 2.0. But it never shows me "UrlEncode" intelisense when I place dot after httpserverutility. Here are the options I see:
HttpServerUtility.Equals
HttpServerUtility.ReferenceEquals
HttpServerUtility.UrlTokenDecode
HttpServerUtility.UrlTokenEncode
That's it, these are the only options I see. Could I possibly ask for small piece of code as an example to use url encode function?
Thanks,
Linda

Categories