您可以創建在Global.asax的兩條路線。事情是這樣的
routes.MapRoute("", "posts/{url}", new { controller = "Home", action = "Posts" });
routes.MapRoute("", "{url}", new { controller = "Home", action = "Posts" });
他們都指向HomeController
和行動Posts
public ActionResult Posts(string url)
{
}
天天看網址,你應該考慮延長RouteBase類
類似的東西應該做的
public class CustomRouting : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData result = null;
string requestUrl = httpContext.Request.AppRelativeCurrentExecutionFilePath;
//Handle the request
//Compile the RouteData with your data
result = new RouteData(this, new MvcRouteHandler());
result.Values.Add("controller", "MyController");
result.Values.Add("action", "MyAction");
result.Values.Add("id", MyId);
}
}
return result;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
//I only need to handle outbound so here is ok
return null;
}
}
在Global.asax中註冊您的自定義路由處理
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new CustomRouting());
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
我想我應該還提到,這應該是全球性的,這意味着用戶應該能夠更改URL(如上所述)的對網站上的其他內容就像/ topics/or/blog/ – hjavaher 2012-04-14 09:37:37
那麼您應該實現自己的路由處理,也許基於DB的表格。看起來實現RouteBase類。 – Iridio 2012-04-14 09:43:45
哇,這是一個很棒的解決方案:)謝謝! – hjavaher 2012-04-14 17:43:22