2012-02-06 124 views
3

我希望用戶在成功註冊/登錄後的URL的用戶名,這樣如何在URL中添加用戶名(即URL重寫)?

http://myweb.com/username/255 
http://myweb.com/username/settings 

我使用ASP.Net重寫模塊,這是我目前使用產生http://myweb.com/home/255重新寫入規則之一:

<rule name="HomeRewrite" stopProcessing="true"> 
    <match url="^home$"/> 
    <conditions> 
     <add input="{URL}" pattern="\.axd$" negate="true"/> 
    </conditions> 
    <action type="Rewrite" url="/home.aspx"/> 
</rule> 

我試着改變自己是這樣的:

<rule name="HomeRewrite" stopProcessing="true"> 
    <match url="^{R:1}/home$"/> 
    <conditions> 
     <add input="{URL}" pattern="\.axd$" negate="true"/> 
    </conditions> 
    <action type="Rewrite" url="/home.aspx"/> 
</rule> 

,但它不工作,我知道我做的WR翁。那麼在這方面有什麼幫助?

+0

爲什麼不使用頁面路由?我有一些頁面路由代碼將用戶名添加到路由數據中。還是你想讓瀏覽器地址欄中的網址改變? – 2012-05-30 22:41:21

回答

2

雖然ASP.NET Routing是MVC的一部分,您可以使用該機制在ASP.Net窗體應用程序,以及(見http://www.4guysfromrolla.com/articles/051309-1.aspx

例如在Global.asax中的無效Application_Start(object sender, EventArgs e) 您可以添加以下代碼

RouteTable.Routes.Add("Users", new Route("{username}/settings", 
new CustomRouteHandler())); 

下,定製類CustomRouteHandler工具IRouteHandler將請求發送到您的設置代碼(視圖)。

只要他通過身份驗證(並將其保留在那裏),您只需實際將用戶重定向到該網址即可。在Global.asax的Application_BeginRequest(object sender, EventArgs e)