当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 基于ASP.NET2.0的非HttpModule山寨版MVC框架的实现

ASP.NET
Asp.net 时间操作基类(支持短日期,长日期,时间差)
asp.net 获取机器硬件信息(cpu频率、磁盘可用空间、内存容量等)
asp.net 数据库备份还原(sqlserver+access)
Asp.Net 数据操作类(附通用数据基类)
Asp.net 弹出对话框基类(输出alet警告框)
Asp.net 文件上传类(取得文件后缀名,保存文件,加入文字水印)
Asp.net Socket客户端(远程发送和接收数据)
Asp.net 字符串操作基类(安全,替换,分解等)
Asp.Net数据输出到EXCEL表格中
asp.net Gridview里添加汇总行
asp.net UpdatePanel的简单用法
asp.net ajaxControlToolkit FilteredTextBoxExtender的简单用法
this connector is disabled错误的解决方法
sql事务应用积累
asp.net Page.Controls对象(找到所有服务器控件)
在asp.NET中字符串替换的五种方法
ASP.NET缓存方法分析和实践示例代码
asp.net 在DNN模块开发中遇到的resx怪问题
ASP.NET State service状态服务的问题解决方法
asp.net 结合mysql存储过程进行分页代码

基于ASP.NET2.0的非HttpModule山寨版MVC框架的实现


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 115 ::
收藏到网摘: n/a

在ASP.Net MVC框架中是使用地址拦截的,虽然很好用,但是装起来太大了,配置也麻烦。本文通过代码实践,在ASP.Net2.0框架下实现一套简易的MVC框架。MVC框架难于构建的地方在于Controller与View的分离以及分离后数据可以方便地传输。为了保持代码的简洁,将使用ashx文件作为Controller,用aspx页面作为View。

讲起来比较费劲,把项目文件放上来,而下面只作一个简单的说明。项目是VS2008的项目,大小15K。

下载地址:DotNetMVC.rar

首先构建一个Controller基类。

以下为引用的内容:


/**
 * author : yurow
 *      
http://birdshover.cnblogs.com
 * description:
 *      
 * history : created by yurow 2009-9-20 7:30:04 
 
*/

using System.Web;
using System.Web.Services;

namespace DotNetMVC.MVC {
    
/// <summary>
    
/// 控制器
    
/// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
    
public abstract class Controller<T, K> : IHttpHandler {
        
/// <summary>
        
/// 当前请求
        
/// </summary>
        protected MyRequest Request;
        
/// <summary>
        
/// 输出
        
/// </summary>
        protected HttpResponse Response;
        
/// <summary>
        
/// 返回到View页面的数据
        
/// </summary>
        protected MvcViewData<T, K> ViewData;
        
/// <summary>
        
/// 控制器名称
        
/// </summary>
        private string controllerName;
        
/// <summary>
        
/// 控制器操作方法
        
/// </summary>
        public abstract void Action();
        
/// <summary>
        
/// 执行请求
        
/// </summary>
        
/// <param name="context"></param>
        public void ProcessRequest(HttpContext context) {
            Request 
= context.Request;
            Response 
= context.Response;
            
//这里可以用反射的方式进行带参数的操作,这里为了简化,去掉了这部分
            
//MethodInfo method = this.GetType().GetMethod("Action", new Type[0]);
            
//if (method == null) {
            
//    throw new NotImplementedException("没有实现!");
            
//}
            
//object data = method.Invoke(this, null) as object;

            ViewData 
= new MvcViewData<T, K>();
            Action();
            context.Items.Add(
"MvcViewData", ViewData);
            context.Server.Transfer(
"~/View/" + ControllerName + ".aspx"false);
        }
        
/// <summary>
        
/// 控制名称,不设置默认为View页面与控制器名称同名
        
/// 比如,在Login.ashx请求中,默认调用View/Login.aspx的页面作为显示页面。
        
/// 当登录成功后,设置其为LoginOK,则会调用View/LoginOK.aspx
        
/// </summary>
        protected string ControllerName {
            
get {
                
if (string.IsNullOrEmpty(controllerName))
                    
return this.GetType().Name;
                
return controllerName;
            }
            
set {
                controllerName 
= value;
            }
        }

        
public bool IsReusable {
            
get {
                
return false;
            }
        }
    }
}

Controller在ProcessRequest方法中调用aspx页面,里面设置了一个虚方法Action在具体的ashx文件中重载。

下面是Default.ashx.cs文件的写法

以下为引用的内容:


sing DotNetMVC.MVC;

namespace DotNetMVC {
    
/// <summary>
    
/// $codebehindclassname$ 的摘要说明
    
/// </summary>
    public class Default : Controller<stringstring> {
        
public override void Action() {

        }
    }
}

在Controller中,还有两个重要的东西一个是传递给View数据用的,一个是显示哪个View的(通过ControllerName这个属性)

原文地址:http://www.cnblogs.com/birdshover/archive/2009/09/20/1570552.html