[Oyster轻量框架]MVC引擎

最近在重构的项目打算采用MVC3来实现,但是研究MVC3的示例代码之后觉得如果是开发一个成熟的项目有很多的别扭,

如示例代码是写在一个项目中,没有真正的实现VIEW 和Controller的分离,目录结构也很死板,支持的皮肤功能居然只是可以换

样式文件而已,不能支持多套独立的模板,比如我的项目就是要开发多个频道,页面完全不同,但是数据信息是使用差不多的,

完全可以复用Controller ,但是还是被限制得死死的,最后就是他自以为很得意的路由设置,居然写在代码里,额,一点点修改都要重新编译,

非常不方便,并且他的理由也很认死理,一条路由匹配上了即使找不到对应的Action 他也不抛给下一条路由去匹配,而是404了,导致这些路由写起来非常麻烦,

需要完全没有重叠和冲突。

不过幸好他提供了其他自定义的方式,SO 我改造了,来看我的项目结构:

我的SITE项目只有VIEW,它是基于模板的,目录结构是 Themes/Theme/Views,
Base模板作为基础模板,将所有的页面放在Base中,如果你再想做一个单独的上海站的特别的首页,你只需要在
Themes下创建一个Shanghai/Site/Index.cshtml.当你系统判断是访问上海站的时候,去调用Shanghai目录下的Index.cshtml
并且改页面中的控件,或者子Action仍旧可以使用Base下的,并不需要做特殊标记,这一切都在引擎中智能匹配了。
而这个模板配置我是放在了配置文件中,同时在代码中也可以设置当前请求要使用的模板。
来看看配置文件:
1 <?xml version="1.0" encoding="utf-8" ?>
2  <Mvc>
3 <Routes>
4 <Route Name="Default" Format="{controller}/{action}/{id}">
5 <DefaultParams controller="Site" action="Index" id="UrlParameter.Optional" />
6 </Route>
7 <Route Name="Action" Format="{action}/{id}">
8 <DefaultParams controller="Site" action="Index" id="UrlParameter.Optional" />
9 </Route>
10 <Route Name="Content" Format="{*pathinfo}">
11 <DefaultParams handler="Oyster.MvcEngine.ContentHandler,Oyster" />
12 </Route>
13 </Routes>
14 <Formats>
15 <ContentFormats>
16 <Format>~/Themes/{1}/{0}</Format>
17 <Format>~/Views/Themes/{1}/{0}</Format>
18 <Format>~/Themes/{1}/Content/{0}</Format>
19 <Format>~/Views/Themes/{1}/Content/{0}</Format>
20 </ContentFormats>
21 <MasterLocationFormats>
22 <Format>~/Themes/{1}/Shared/{0}</Format>
23 <Format>~/Views/Themes/{1}/Shared/{0}</Format>
24 </MasterLocationFormats>
25 <ViewLocationFormats>
26 <!-- 重长到短,从参数多到少 -->
27 <Format>~/Themes/{1}/Site/{2}/{0}.cshtml</Format>
28 <Format>~/Themes/{1}/Site/{0}.cshtml</Format>
29 <Format>~/Themes/{1}/Shared/Controls/{2}/{0}.cshtml</Format>
30 <Format>~/Themes/{1}/Shared/Controls/{0}.cshtml</Format>
31 <Format>~/Themes/{1}/Shared/Common/{2}/{0}.cshtml</Format>
32 <Format>~/Themes/{1}/Shared/Common/{0}.cshtml</Format>
33 <Format>~/Themes/{1}/Shared/{2}/{0}.cshtml</Format>
34 <Format>~/Themes/{1}/Shared/{0}.cshtml</Format>
35 <!--With Views-->
36 <Format>~/Views/Themes/{1}/Site/{2}/{0}.cshtml</Format>
37 <Format>~/Views/Themes/{1}/Site/{0}.cshtml</Format>
38 <Format>~/Views/Themes/{1}/Shared/Controls/{2}/{0}.cshtml</Format>
39 <Format>~/Views/Themes/{1}/Shared/Controls/{0}.cshtml</Format>
40 <Format>~/Views/Themes/{1}/Shared/Common/{2}/{0}.cshtml</Format>
41 <Format>~/Views/Themes/{1}/Shared/Common/{0}.cshtml</Format>
42 <Format>~/Views/Themes/{1}/Shared/{2}/{0}.cshtml</Format>
43 <Format>~/Views/Themes/{1}/Shared/{0}.cshtml</Format>
44
45 <!-- 重长到短,从参数多到少 -->
46 <Format>~/Themes/{1}/Site/{2}/{0}.aspx</Format>
47 <Format>~/Themes/{1}/Site/{0}.aspx</Format>
48 <Format>~/Themes/{1}/Shared/Controls/{2}/{0}.ascx</Format>
49 <Format>~/Themes/{1}/Shared/Controls/{0}.ascx</Format>
50 <Format>~/Themes/{1}/Shared/Common/{2}/{0}.ascx</Format>
51 <Format>~/Themes/{1}/Shared/Common/{0}.ascx</Format>
52 <Format>~/Themes/{1}/Shared/{2}/{0}.ascx</Format>
53 <Format>~/Themes/{1}/Shared/{0}.ascx</Format>
54 <!--With Views-->
55 <Format>~/Views/Themes/{1}/Site/{2}/{0}.aspx</Format>
56 <Format>~/Views/Themes/{1}/Site/{0}.aspx</Format>
57 <Format>~/Views/Themes/{1}/Shared/Controls/{2}/{0}.ascx</Format>
58 <Format>~/Views/Themes/{1}/Shared/Controls/{0}.ascx</Format>
59 <Format>~/Views/Themes/{1}/Shared/Common/{2}/{0}.ascx</Format>
60 <Format>~/Views/Themes/{1}/Shared/Common/{0}.ascx</Format>
61 <Format>~/Views/Themes/{1}/Shared/{2}/{0}.ascx</Format>
62 <Format>~/Views/Themes/{1}/Shared/{0}.ascx</Format>
63 </ViewLocationFormats>
64 <PartialViewLocationFormats></PartialViewLocationFormats>
65 <AreaMasterLocationFormats></AreaMasterLocationFormats>
66 <AreaViewLocationFormats></AreaViewLocationFormats>
67 <AreaPartialViewLocationFormats></AreaPartialViewLocationFormats>
68 </Formats>
69 <Themes>
70 <Theme Default="true" Engine="Razor">Base</Theme>
71 <Theme Current="true" Engine="Razor">Shanghai</Theme>
72 </Themes>
73
74  </Mvc>
哈哈,有点意思吧,总之我是重写了几乎所有的MVC组件,Application,Route机制,ControllerFactory 等,我自己还是觉得非常满意的,
好吧调调胃口,慢慢分享,等框架完全好了的时候,我会开源的哦~~

posted on 2011-04-23 01:47  oyster.oy  阅读(788)  评论(2编辑  收藏  举报

导航