【发布时间】:2010-04-07 23:48:50
【问题描述】:
我希望创建一个 MVC 站点,该站点可以使用路由完全控制 url 结构。
具体要求是:
www.mysite.com/ = 主页(家庭控制器)
www.mysite.com/common/about = 内容页面(通用控制器)
www.mysite.com/common/contact = 内容页面(通用控制器)
www.mysite.com/john = twitter 风格的用户页面(动态控制器)
www.mysite.com/sarah = twitter 风格的用户页面(动态控制器)
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 重定向,我有一个定义旧页面及其新页面 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)如果用户名在用户列表(数据库)中,则显示动态控制器的Index ActionResult。
3) 如果未找到用户名,请在高级列表中查找用户名
4) 如果用户名在 Premium 列表(数据库)中是 fund,则显示 Preium 控制器的 Index ActionResult。
5) 如果一切都失败,则跳转到 404 页面(会要求用户注册)
这可能吗?
查找用户两次不利于性能?
如何在不重定向的情况下做到这一点?
【问题讨论】:
标签: asp.net-mvc seo asp.net-mvc-routing