您需要创建自定义路由。
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 }
);
}
}