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

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

2011011115172136.png

第二步、添加Route

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

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

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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键^_^

转载于:https://www.cnblogs.com/cnfblog/archive/2011/01/11/1932935.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值