您需要創建自定義路線。
public class AdminRoute : Route
{
public AdminRoute(string url, IRouteHandler routeHandler)
: base(url, routeHandler) {}
public AdminRoute(string url, RouteValueDictionary defaults,
IRouteHandler routeHandler) : base(url, defaults, routeHandler) {}
public AdminRoute(string url, RouteValueDictionary defaults,
RouteValueDictionary constraints, IRouteHandler routeHandler)
: base(url, defaults, constraints, routeHandler){}
public AdminRoute(string url, RouteValueDictionary defaults,
RouteValueDictionary constraints, RouteValueDictionary dataTokens,
IRouteHandler routeHandler)
: base(url, defaults, constraints, dataTokens, routeHandler){}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData data = base.GetRouteData(httpContext);
if (data != null)
data.Values["controller"] = "Admin" + data.Values["controller"];
return data;
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new AdminRoute("Admin/{controller}/{action}/{id}",
new RouteValueDictionary(
new { controller = "Home", action="Index",
id = UrlParameter.Optional }), new MvcRouteHandler()));
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
您是否考慮過使用區域? – 2013-05-12 14:07:52
如果可能,請重新創建控制器並將其重命名爲ie。 'Adminproduct'到'Product',然後你可以簡單地做到這一點; 'routes.MapRoute(「AdminProduct」,「Admin/{controller}/{action}」,new {action =「Index」});' – 2013-05-12 15:53:20