2009-09-03 32 views
0

我要做到以下幾點:淨MVC:綁定路線值會話值

用戶應該有自己的子網站上我的網站。爲此我已經添加了一個新的途徑:

routes.MapRoute(
    "ShopEntered", 
    "{username}/{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", id = "" } 
); 

當網站被調用,第一次我想要的用戶名值存儲在會話,然後綁定會話價值路線的用戶名部分,所以當我調用Url.Action或Html.ActionLink時,我不必每次都手動設置它。

有誰知道我怎麼能意識到這一點?

回答

0

最簡單的方法可能是編寫自己的幫手方法,並使用它們而不是Html.ActionLinkUrl.Action。例如:

public static class UserUrlExtensions 
{ 
    // Create whichever overloads you want 
    public static string UserActionLink(this HtmlHelper helper, string linkText, RouteValueCollection routeValues) 
    { 
     // Get whatever value it is you want from the current context 
     routeValues["User"] = helper.ViewContext.HttpContext.Current.User.Identity.Name; 

     return helper.ActionLink(linkText, routeValues); 
    } 
} 

我不保證它會不經修改工作,但你怎麼做工作的想法...