非常简单的实现MVC多级目录

第一步:创建目录结构(看需求来定)

第二步、添加Route

routes.MapRoute(
"HttpApplication",
"HttpApplication/{action}",
new { action = "Index" }
);

第三步、重写HttpApplicationController的OnActionExecuting事件(关键)

 

代码
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
string actionName = this.RouteData.Values["action"].ToString() + ".aspx";
this.RouteData.Values["action"] = "~/Views/SystemWeb/HttpApplication/" + actionName;
base.OnActionExecuting(filterContext);
}

第四步、按F5键^_^

 

 

 

 

使用Filter实现上述功能

一、自定义一个继承自ActionFilterAttribute的类,重写OnActionExecuting事件,代码如下:

 public class CustomRouting : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string value = filterContext.RouteData.Values["action"].ToString() + ".aspx";
            filterContext.RouteData.Values["action"] = Folder + value;
            base.OnActionExecuting(filterContext);
        }

        public CustomRouting(string folder)
        { this.Folder = folder; }

        public string Folder { get; set; }
    }

二、在Controller中标记属性

 [CustomRouting("~/Views/Home/Routing/")]
    public class HomeController : Controller

 

三、按F5键^_^

posted @ 2011-01-11 15:25  CNF  阅读(2753)  评论(8编辑  收藏  举报