体验不一样的MVC,让开发更加迅捷(转http://www.cnblogs.com/imfunny/archive/2012/02/06/mvc.html)

Coordinator
Feb 8, 2012 at 7:56 AM

去年说了要做一些开源项目,一直都忙着玩,也没发布。今年借此机会先尝鲜体现下犀利的C#吧。

在上上一篇的时候介绍了MVC,这一篇就来分享下自己的mvc项目。虽然就一百多行代码。不过小即使美。废话不多说,看代码。

首先MvcHttpHandler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Web;
using System.Linq;
using System.Web.SessionState;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using StructureMap;
  
namespace Dapper.Web.Mvc
{
    /// <summary>
    /// 拦截请求,并对请求进行处理
    /// </summary>
    public class MvcHttpHandler : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// 此部分请求不共享,并不保证绝对的线程安全。如果设置true,测需要对控制器设置HybridHttpOrThreadLocalScoped或者HttpContextScoped
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
  
        /// <summary>
        /// 隐示实现IHttpHandler接口
        /// </summary>
        /// <remarks>
        /// 此部分为了兼容与IHttpHandler接口,以及MoqHttpContextBase对象
        /// </remarks>
        /// <param name="httpContext">HttpContext请求对象</param>
        public virtual void ProcessRequest(HttpContext httpContext)
        {
            this.HttpContext = new HttpContextWrapper(httpContext);
            this.ProcessRequest();
        }
  
        /// <summary>
        /// 执行请求
        /// </summary>
        public virtual void ProcessRequest()
        {
            var request = new RequestContext(this.HttpContext);
            IController controller = null;
            try
            {
                controller = ObjectFactory.GetNamedInstance<IController>(request.ControllerName);
            }
            catch
            {
                throw new NullReferenceException(string.Format("未查询到{0}的实例", request.ControllerName));
            }
            finally
            {
                controller.Execute(request);
            }
            return;
           
        }
  
  
        /// <summary>
        /// HttpContext的抽象
        /// </summary>
        public HttpContextBase HttpContext
        {
            get;
            set;
        }
  
        #region IHttpHandler
  
        /// <summary>
        /// 显示实现IHttpHandler接口,对方法进行指定。
        /// </summary>
        bool IHttpHandler.IsReusable
        {
            get
            {
                return this.IsReusable;
            }
  
        }
  
        /// <summary>
        /// 显示实现IHttpHandler接口,对方法进行指定。
        /// </summary>
        /// <param name="context">HttpContext请求对象</param>
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            this.ProcessRequest(context);
            return;
        }
  
        #endregion
    }
}

 再次RequestContext