查询字符串作为 url 的一部分

发布于 01-01 11:52 字数 266 浏览 2 评论 0 原文

我有一个带有查询字符串 http://www.sample.com?q=asdasdsdasd 的网址。是否可以修改查询字符串,以便我可以用 /myaccount 替换它,即最后的 url 看起来像 http ://www.sample.com/myaccount

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

本宫微胖 2025-01-08 11:52:54
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);
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);
鱼窥荷 2025-01-08 11:52:54

查看 url重写。您可能无法直接访问 /myaccount,但您可以整理您的网址,使它们对 SEO 更具可读性和意义。

您将能够使用 使您的 url 看起来类似于以下内容:

www.sample.com/account/asdaasdasd

如果您完全丢失了查询字符串,您将根本无法访问它。除非您实现了某种形式的临时代码来获取查询字符串,将其存储在会话中,然后重定向到您的 /myaccount url 并将其返回。

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.

心碎的声音 2025-01-08 11:52:54

我认为你指的是 URL 重写。

这是一篇关于 URL 重写的常用博客文章:

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

或者如果您有 IIS7 ,现在变得更容易:

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


在的条款不过,将 ?q=asdasdsdasd 更改为 /myaccount ,我不明白。第一个 URL 看起来像一个典型的搜索查询,第二个 URL 可能会使用 cookie 等来获取变量(因为它用于用户帐户?)。

但是可以使用 URL 重写,这样如果您的用户配置文件的 URL 如下所示:

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

这可以使用 johnsmith 进行重写 部分作为变量,例如:

www.sample.com/user/johnsmith

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

心在旅行 2025-01-08 11:52:54

通过简单的字符串操作,您可以这样做:

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

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));
薄荷梦 2025-01-08 11:52:54

在 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]);

}

然后使用此地址
http://www.sample.com/myaccount/asdasdasd

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文