您的位置:首页 > 其它

简单实际的方式分隔Admin 区域

2011-03-22 21:01 211 查看

Add these 9 lines of code to your app

public class AdminRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
RouteData routeData = requestContext.RouteData;
routeData.Values["controller"] = "Admin" + requestContext.RouteData.GetRequiredString("controller");
return new MvcHandler(requestContext);
}
}

Add a new route in your Global.asax.cs

routes.Add(
"AdminRoutes", // Route name
new Route(
"Admin/{controller}/{action}/{id}", // URL with parameters
new RouteValueDictionary(new { controller="Video", action = "Index", id=""}),
new AdminRouteHandler()) // Parameter defaults
);

Tada! You’re done! There’s one catch…

Controllers in the Admin area must start with the “Admin” prefix.



You then create appropriate folders for views as usual.



This is a bit of a hack, but I like the fact that it requires very little code and it’s very simple :) Also, it would be very easy to tweak this code to allow for general partitioning of controllers by prefix, so you could have a “Admin” area, a “Mobile” area etc.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: