开发者

Getting URL of rewritten request?

All the users on my site have public-facing profile pages. I am using URL rewriting to change urls from the form http://mysite.com/profile.aspx?id=sdsdfsdsdfsdfdsdffsdfsdf into http://mysite.com/Username like this (in my global.asax file):

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)
            {
            }
        }
    }
}

This works fine. However, if I do a postback to the server, the URL changes fr开发者_运维百科om something like /username to the form /profile?id=5ab47aa3-3b4d-4de6-85df-67527c9cdb52&, which I want to hide from the user.

I thought about doing something like Response.Redirect(Request.RawUrl); to sent the user back to the right page. However, Request doesn't seem to contain any information about the desired URL.

Is there any way to find the pre-rewritten URL?


What platform are you on? If IIS 7 you would be best off using IIS URL Rewrite 2.0. You can find some suggestions on dealing with postback using this.


I suggest you use Routing, that way you can write your code the ASP.NET way but still have the Urls you want:

Routing with ASP.NET Web Forms

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜