我期待產生一個MVC網站,使用路由完全控制的網址結構。動態'微博風格'網址與ASP.NET
的具體要求是:
www.mysite.com/ =主頁(家庭控制器)
www.mysite.com/common/about =內容頁(通用控制器)
WWW .mysite.com /普通/接觸=內容頁面(公共控制器)
www.mysite.com/john =微樣式的用戶頁面(動態控制器)
www.mysite.com/sarah =微樣式的用戶頁面(動態控制器)
www.mysite.com/me =溢價樣式的用戶頁面(高級控制器)
www.mysite.com/oldpage.html = 301重定向到新的頁面
www.mysite.com/oldpage.asp?id=3333 = 301重定向到新的頁面
我的路線如下所示:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Common",
"common/{action}/{id}",
new { controller = "common", action = "Index", id = "" }
);
routes.MapRoute(
"Home",
"",
new { controller = "Home", action = "Index", id = "" }
);
routes.MapRoute(
"Dynamic",
"{id}",
new { controller = "dynamic", action = "Index", id = "" }
);
爲了處理301 rredirct,我有一個數據庫定義舊頁面和他們的新頁面URL和一個存儲處理查找程序。該代碼(處理器)看起來是這樣的:
公共類AspxCatchHandler:IHttpHandler的,IRequiresSessionState {
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
if (context.Request.Url.AbsolutePath.Contains("aspx") && !context.Request.Url.AbsolutePath.ToLower().Contains("default.aspx"))
{
string strurl = context.Request.Url.PathAndQuery.ToString();
string chrAction = "";
string chrDest = "";
try
{
DataTable dtRedirect = SqlFactory.Execute(
ConfigurationManager.ConnectionStrings["emptum"].ConnectionString,
"spGetRedirectAction",
new SqlParameter[] {
new SqlParameter("@chrURL", strurl)
},
true);
chrAction = dtRedirect.Rows[0]["chrAction"].ToString();
chrDest = dtRedirect.Rows[0]["chrDest"].ToString();
chrDest = context.Request.Url.Host.ToString() + "/" + chrDest;
chrDest = "http://" + chrDest;
if (string.IsNullOrEmpty(strurl))
context.Response.Redirect("~/");
}
catch
{
chrDest = "/";// context.Request.Url.Host.ToString();
}
context.Response.Clear();
context.Response.Status = "301 Moved Permanently";
context.Response.AddHeader("Location", chrDest);
context.Response.End();
}
else
{
string originalPath = context.Request.Path;
HttpContext.Current.RewritePath("/", false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
HttpContext.Current.RewritePath(originalPath, false);
}
}
#endregion
}
這是非常簡單的查找用戶,實際上上面的代碼做到這一點。我的問題在於動態/高級部分。
我努力做到以下幾點:在動態控制器
1),查找用戶名。 2)如果用戶名在用戶列表(數據庫)中,則顯示動態控制器的索引動作結果。
3)如果沒有找到用戶名,查找在保費名單
4)如果用戶名是基金溢價列表(數據庫的用戶名),然後顯示Preium控制器的指數的ActionResult。
5)如果一切都失敗跳轉到404頁面(這將要求用戶註冊)
這可能嗎?
向上查找用戶兩次對於性能來說是個壞主意?
如何在不重定向的情況下執行此操作?
我認爲這也是一個很好的方法。把所有的用戶放在一張桌子上,並有一個標誌。根據溢價標誌渲染適當的視圖。 如果用戶不存在,404 ... – Cymen 2010-04-08 04:29:43
在一個理想的世界裏,這將是一張表格,也是一個顯示數據不同的簡單問題。 但事實並非如此,我在邏輯的同一控制器中有兩個ActionResults和兩個視圖。 我以爲ActionResult不會這樣做? – Gavin 2010-04-08 09:27:04
您是否可以不創建數據庫級別視圖來合併用戶數據?如果沒有,那麼你應該可以使用RedirectToAction - 請參見清單3中的示例:http://www.asp.net/Learn/mvc/tutorial-03-cs.aspx – Jonathan 2010-04-08 11:30:31