---
title: "在ASP.NET下做了一个实验MVC的小东西，希望能得到更多的参考意见"
author: "ipconfiger"
date: "2007-10-21T19:53:00Z"
tags: []
slug: "asp-net-mvc"
draft: false
lastmod: "2007-10-21T19:53:00Z"
id: 677406
url: "https://ipconfiger.iteye.com/blog/677406"
---

星期五下班前一时兴起，对MVC发表了一点看法。后来就想干脆弄个小东西来验证一些自己的想法帮组理清思路。所要源代码的免了，3个小时的急就章实在是乱得可以，既没有好的架构，也没有任何代码质量可言，主要是实现了对MVC的一点想法。所以拿出来大家给参考一下。申明：不是典型的MVC

在这里我参考了Web.Py的一些做法，首先我们将Url当作一种资源，一个典型的Url，如下：  
http://www.x.com/Index/view/2007/10/21  
我们将从/Index/Topic/2007/10/21开始的的部分看作是有语义的，这里将Index映射到Controller类Index，Topic则表示这一次调用的行为，也就是调用Index类的Topic方法。  
后面的/2007/10/21则是参数。而这些参数作为View方法的参数被传递给Controller，也就是这里Topic方法可以重载，如果找不到参数相对应的方法就直接调用没有参数的Topic方法。  
Index类被作为Controller，至于View，随便了，可以直接用StringTemplate或者其他任何模板引擎，图方便的话直接Redirect到ASPX页面也行。  
使用的方法：  
首先Copy SpectraAction.DLL到bin目录。然后在Web.Config里添加：  
  &lt;configSections&gt;  
    &lt;sectionGroup name="SpectraAction"&gt;  
      &lt;section name="Common" type="System.Configuration.SingleTagSectionHandler"/&gt;  
      &lt;section name="Urls" type="System.Configuration.DictionarySectionHandler"/&gt;  
    &lt;/sectionGroup&gt;  
  &lt;/configSections&gt;  
  &lt;SpectraAction&gt;  
    &lt;Common Base="/TestSpectra" DefaultAction="Proccess" Assembly="" /&gt;  
    &lt;Urls&gt;  
      &lt;add key="/index" value="Controller.Index"/&gt;  
    &lt;/Urls&gt;  
  &lt;/SpectraAction&gt;  
在Url里面的项就是注册Url到Controller的映射关系的。Common里的Base定义了Url的基础，因为在很多时候在根下有一些目录比较特殊，为了不影响这些目录，所以设置了这个参数用来设置一个基础来避开。所以这里要从 /TestSpectra之后的部分才开始具备语义。  
然后就是实现Controller.Index这个类：

 1![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/None.gif)namespace Controller  
 2![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ContractedBlock.gif)![](http://www.cnblogs.com/Images/dot.gif){  
 3![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/InBlock.gif)    public class Index:MasterController  
 4![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ContractedSubBlock.gif)    ![](http://www.cnblogs.com/Images/dot.gif){  
 5![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/InBlock.gif)        public Index()  
 6![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ContractedSubBlock.gif)        ![](http://www.cnblogs.com/Images/dot.gif){  
 7![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/InBlock.gif)            //  
 8![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/InBlock.gif)            // TODO: 在此处添加构造函数逻辑  
 9![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/InBlock.gif)            //  
10![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
11![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/InBlock.gif)        public void Proccess()  
12![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ContractedSubBlock.gif)        ![](http://www.cnblogs.com/Images/dot.gif){  
13![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/InBlock.gif)            this.AppendText("Test");  
14![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
15![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/InBlock.gif)        public void Proccess(string A)  
16![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ContractedSubBlock.gif)        ![](http://www.cnblogs.com/Images/dot.gif){  
17![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/InBlock.gif)            this.AppendText("Test"+A);  
18![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
19![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
20![](http://images.cnblogs.com/cnblogs_com/alexander-lee/Images/OutliningIndicators/ExpandedBlockEnd.gif)}

最后我们在浏览器里输入 http://localhost/TestSpectra/Index  
这个时候输出 Test 因为设置了默认的行为，所以自动调用了Proccess方法。  
如果输入 :  
http://localhost/TestSpectra/Index/Proccess/ActionA/  
这个时候就会调用void Proccess(string A)这个方法，输出了字符串TestActionA

就是这个样子咯，模型的话随便什么都行，视图的话随便用什么模板引擎都行  
![](http://sc.chinaz.com/Files/pic/face/350_s.gif)