gpt4 book ai didi

c# - 获取重写请求的 URL?

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:09 24 4
gpt4 key购买 nike

我网站上的所有用户都有面向公众的个人资料页面。我正在使用 URL 重写将 url 从表单 http://mysite.com/profile.aspx?id=sdsdfsdsdfsdfdsdffsdfsdf 更改为 http://mysite.com/Username像这样(在我的 global.asax 文件中):

static Regex _handleRegex1 = new Regex("/(?<hndl>[\\w]+)/?$", RegexOptions.Compiled);

void Application_BeginRequest(object sender, EventArgs e)
{

System.Text.RegularExpressions.Match handleMatch = _handleRegex1.Match(Request.Url.LocalPath);
if(handleMatch.Success){
String handle = handleMatch.Groups[1].Value;

using (SqlQuery query = new SqlQuery("[dbo].[sp_getUserIdByHandle]"))
{
try
{
query.AddParameter("@handle", handle, System.Data.SqlDbType.NVarChar, false);
query.AddParameter("@userId", new Guid(), System.Data.SqlDbType.UniqueIdentifier, true);

query.ExecuteNonQuery();

Object userId = query.GetOutParameter("@userId");

if (userId == DBNull.Value)
{
Response.Redirect("~/default.aspx");
}
else
{
Context.RewritePath(string.Format("~/profile.aspx?id={0}&{1}", userId, Request.QueryString));
}
}
catch (Exception ex)
{
}
}
}
}

这很好用。但是,如果我对服务器进行回发,URL 会从类似 /username 的形式更改为 /profile?id=5ab47aa3-3b4d-4de6-85df-67527c9cdb52& ,我想对用户隐藏。

我想过做一些类似 Response.Redirect(Request.RawUrl); 的事情来让用户回到正确的页面。但是,Request 似乎不包含有关所需 URL 的任何信息。

有什么办法可以找到预先重写的 URL 吗?

最佳答案

你在什么平台上?如果 IIS 7 你最好使用 IIS URL Rewrite 2.0 .你可以找到一些 suggestions on dealing with postback using this .

关于c# - 获取重写请求的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7727735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com