0

I have a url with querystring http://www.sample.com?q=asdasdsdasd . Will it be possible to modify the querystring so that I could replace it with /myaccount i.e at the end the url will look like http://www.sample.com/myaccount.

2
  • 2
    How will you pass in the parameter? Commented Feb 1, 2012 at 9:49
  • @Oded The /myaccount should have the parameter as well Commented Feb 1, 2012 at 9:55

5 Answers 5

2
string destUrl = string.Format("{0}://{1}{2}/",Request.Url.Scheme,Request.Url.Authority,Request.Url.AbsolutePath);
if (destUrl.EndsWith("/"))
    destUrl = destUrl.TrimEnd(new char[] { '/' });
if (!string.IsNullOrEmpty(Request.QueryString["paramName"])) {
    destUrl = string.Format("{0}?paramName={1}", destUrl, "paramValueHere");
Response.Redirect(destUrl);
Sign up to request clarification or add additional context in comments.

Comments

1

Check out url rewriting. You may not be able to achieve the /myaccount direct, but you can tidy up your urls, make them more readable and meaningful for SEO.

You will be able to use to allow your url to look similar to the following :

www.sample.com/account/asdaasdasd

If you lose the query string all together you won't be able to access it at all. Unless you implemented some form of interim code that will get the query string, store it in a session and then redirect to your /myaccount url and get it back there.

Comments

0

I think you are referring to URL Rewriting.

This is quite a commonly used blog post regarding URL rewriting:

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

Or if you have IIS7, its now been made easier:

http://www.iis.net/download/urlrewrite


In terms of changing ?q=asdasdsdasd to /myaccount though, I don't understand. The first URL seems like a typical search query, and the second is a URL which would probably use cookies etc to pick up the variables (as its for a user account?).

But URL Rewriting can be used so that if you have a user profile with a URL like:

www.sample.com?userprofile.aspx?user=johnsmith

This can be rewritten, using the johnsmith part as a variable like:

www.sample.com/user/johnsmith

Comments

0

With simple string manipulation you could do it as:

string urlWithQuerystring = "http://www.sample.com?q=asdasdsdasd";
int queryStringPos = urlWithQuerystring.IndexOf("?");
string newUrl = String.Format("{0}/myaccount/", urlWithQuerystring.Substring(0, queryStringPos));

Comments

0

Use the this Code in your Global.asax:

void Application_BeginRequest(object sender, EventArgs e)
{

    string[] parts = Request.RawUrl.Split(new char[]{'/'});

    if(Part[1] == "myaccount"))
        Context.RewritePath("http://www.sample.com?q=" + Part[2]);

}

and then use this address http://www.sample.com/myaccount/asdasdasd

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.