zoukankan      html  css  js  c++  java
  • CJCMS系列说说项目中的插件思想(2)

      今天是星期天,那我就赶紧多多的写一些吧。

      上一次的一篇文章,不知道大家有没有看懂,要是没有看懂,请回去慢慢的嚼透吧,我觉得认真的看,有一定的基础应该能够看懂。

      上面一讲我留下一个疑问,那就是一句编译后指令,有一些MVC基础的人,应该能够看出端倪,要是你没有看懂,那就认真的听我道来吧。

      

    1 xcopy /s /y "$(ProjectDir)bin\*" "$(SolutionDir)CJCMS.Web\bin\"
    2 xcopy /s /y "$(ProjectDir)Content\*" "$(SolutionDir)CJCMS.Web\Content"
    3 xcopy /s /y "$(ProjectDir)Scripts\*" "$(SolutionDir)CJCMS.Web\Scripts"
    4 xcopy /s /y /i "$(ProjectDir)Views\*" "$(SolutionDir)CJCMS.Web\Views\Blog"

      第四行中那句代码,我解释一下,怕大家对于xcopy不大理解,这句主要就是在编译之后把,项目的Views目录的文件复制到解决方案文件

    CJCMS.Web\Views\Blog下,谁是CJCMS.Web,CJCMS.Web就是主项目,也就是把插件的Views都拷贝到CJCMS.Web下的Views/Blog去了,但是大家看一下,要是这样的话,主项目的目录结构成什么样子了。

     
    
    

      主项目的Views目录有三层,而不是两个层次,这样子如何访问到Views/Blog/Blog/Home.aspx呢,插件中的Controller结构你看一下是正常的目录,那我们遇到了一个问题,就是如何使得MVC支持多层目录。

      我在Framework中写了这么一个类,继承一下MVC的ActionFilterAttribute,来拦截到请求,然后跳转到你想要跑去的多层目录去,Folder变量就是Views多层目录中你多出来的目录,多出来就是原本MVC是两层目录,而现在变成了多层目录,在浏览器中输入localhost/Blog/Home,如何能够访问到我上面例子的三层目录呢,我其实多了一层目录Blog/,那你就可以在Controller中添加属性了。

    // 作者:                    不要理我 CJ 
    // 邮件:               869722304@qq.com(仅仅支持商业合作洽谈)
    // 创建时间:                2012-08-8
    // 最后修改时间:            2012-08-11
    // 
    // 未经修改的文件版权属于原作者所有,但是你可以阅读,修改,调试.本项目不建议商用,不能确保稳定性
    // 同时由于项目引起的一切问题,原作者概不负责。
    //
    // 本项目所引用的所有类库,仍然遵循其原本的协议,不得侵害其版权
    //
    // 您一旦下载就视为您已经阅读此声明。
    //
    // 您不可以移除项目中所有的声明。
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    
    namespace CJCMS.Framework.Routes
    {
        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怎么用这个呢?
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.Mvc;
     6 using CJCMS.Framework.Routes;
     7 using CJCMS.Contracts.DTO;
     8 //using CJCMS.Plugin.Blog.BlogServiceReference;
     9 using CJCMS.Plugin.Blog.BlogS;
    10 using CJCMS.Contracts.DTO.Blog;
    11 using System.Threading;
    12 
    13 
    14 namespace CJCMS.Plugin.Blog.Controllers
    15 {
    16     [HandleError]
    17     [CustomRouting("~/Views/Blog/Blog")]
    18     public class BlogController : Controller
    19     {
    20        
    21         public ActionResult Home()
    22         {
    23             ViewData["Message"] = "欢迎使用 ASP.NET MVC!";
    24 
    25 
    26             BlogServiceClient service = new BlogServiceClient();
    27 
    28             try
    29             {
    30                 BlogDspModel b = new BlogDspModel();
    31                 b.Title = Guid.NewGuid().ToString();
    32                 b.Body = "123";
    33                 b.Created = DateTime.Now;
    34                 b.Id = Guid.NewGuid().ToString();
    35                 service.Add(b);
    36             }
    37             catch (Exception ee)
    38             {
    39                 ViewData["Message"] += ee.Message;
    40             }
    41 
    42             IList<BlogDspModel> il = service.Fetch();
    43             service.Close();
    44             return View();
    45         }
    46 
    47        
    48 
    49         [HttpGet]
    50         public ActionResult Home(string id,string k)
    51         {
    52             ViewData["Message"] = "欢迎使用 ASP.NET MVC!";
    53             BlogServiceClient service = new BlogServiceClient();
    54             try
    55             {
    56                 BlogDspModel b = new BlogDspModel();
    57                 b.Title = Guid.NewGuid().ToString();
    58                 b.Body = "123";
    59                 b.Created = DateTime.Now;
    60                 b.Id = Guid.NewGuid().ToString();
    61                 service.Add(b);
    62             }
    63             catch (Exception ee)
    64             {
    65                 ViewData["Message"] += ee.Message;
    66             }
    67             IList<BlogDspModel> il = service.Fetch();
    68             service.Close();
    69             return View();
    70         }
    71 
    72         public ActionResult About()
    73         {
    74             return View();
    75         }
    76     }
    77 }

      第17行你已经看到了,怎么去处理,使得MVC支持多层目录,好啦这一讲也结束了。

      下面,我会开始讲讲我写得例子的整体的架构,也就是开始讲DDD了,我的项目设计了,你准备好了吗?

      这个项目,主要使用的技术:ORM(NHibernate),MVC,WCF,DTO-DO Mapping,DDD设计理念,单例模式等等。

      希望你能够期待,要是大家感兴趣,我会选择开源,给大家看看,共同学习。

      Just waiting. Go on  coding.

  • 相关阅读:
    Closing File Descriptors
    Redirecting stdout using exec
    Debugging on part(s) of the script
    Android深入浅出之Audio 第一部分 AudioTrack分析
    svn在linux下的使用(svn命令)[转]
    Android深入浅出之Binder机制
    bash中获取文件路径和文件命的函数
    Android深入浅出之Audio 第一部分 AudioTrack分析
    Shell的脚本编程
    Android深入浅出之Surface
  • 原文地址:https://www.cnblogs.com/ntcj/p/2635264.html
Copyright © 2011-2022 走看看