mvc模拟实现
.定义httpmodule
<system.webServer>
<modules>
<add name="UrlRoutingModule" type="WebApp.UrlRoutingModule,WebApp"/>
</modules>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
.对应的mvc类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
using System.Web.UI;
using System.Web.UI.WebControls; namespace WebApp
{
/// <summary>
/// 二次封装的请求上下文:包括原始的HttpContextBase和经过提取的路由数据对象
/// </summary>
public class RequestContext
{
public virtual HttpContextBase HttpContext { get; set; }
public virtual RouteData RouteData { get; set; }
}
public interface IRouteHandler
{
IHttpHandler GetHttpHandler(RequestContext requestContext);
}
public abstract class RouteBase
{
public abstract RouteData GetRouteData(HttpContextBase httpContext);
}
public class RouteData
{
public IDictionary<string, object> Values { get; private set; }
public IDictionary<string, object> DataTokens { get; private set; }
public IRouteHandler RouteHandler { get; set; }
public RouteBase Route { get; set; }
public RouteData()
{
this.Values = new Dictionary<string, object>();
this.DataTokens = new Dictionary<string, object>();
this.DataTokens.Add("namespaces", new List<string>());
}
public string Controller
{
get
{
object controllerName = string.Empty;
this.Values.TryGetValue("Controller", out controllerName);
return controllerName.ToString();
}
}
public string ActionName
{
get
{
object actionName = string.Empty;
this.Values.TryGetValue("Action", out actionName);
return actionName.ToString();
}
} }
public class Route : RouteBase
{
public IRouteHandler RouteHandler { get; set; }
public string Url { get; set; }
public IDictionary<string, object> DataTokens { get; set; }
public Route()
{
this.DataTokens = new Dictionary<string, object>();
this.RouteHandler = new MvcRouteHandler();
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
IDictionary<string, object> variables;
//如 AppRelativeCurrentExecutionFilePath的值~/home/index,前两个是~/我们从第三位取值,
//所以substring(2)的结果是home/index match返回返回符合路由规则的路由参数这里 字典里controller:"home",action:"index"
if (this.Match(httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(), out variables))
{
RouteData routeData = new RouteData();
foreach (var item in variables)
{
routeData.Values.Add(item.Key, item.Value); }
foreach (var item in DataTokens)
{
routeData.DataTokens.Add(item.Key, item.Value); }
routeData.RouteHandler = this.RouteHandler;
return routeData;
}
return null;
} protected bool Match(string requestUrl, out IDictionary<string, object> variables)
{
variables = new Dictionary<string, object>();
string[] strArray1 = requestUrl.Split('/');
string[] strArray2 = this.Url.Split('/');//其中的某条路由规则
if (strArray1.Length != strArray2.Length)
{
return false;
}
for (int i = ; i < strArray2.Length; i++)
{
if (strArray2[i].StartsWith("{") && strArray2[i].EndsWith("}"))
{
variables.Add(strArray2[i].Trim("{}".ToCharArray()), strArray1[i]);
}
}
return true;
}
} public class RouteTable
{
public static RouteDictionary Routes { get; private set; }
static RouteTable()
{
Routes = new RouteDictionary();
} } public class RouteDictionary : Dictionary<string, RouteBase>
{
public RouteData GetRouteData(HttpContextBase httpContext)
{
//便利global.asax添加的路由规则
foreach (RouteBase route in this.Values)
{
//比如获取得到其中的某一条路由规则 "{Controller}/{Action}" 比如我们请求地址/home/index 对应的数据层次就像routeData.Value=["Controller":"home","Action":"index"]
//routeData.RouteHandler=MvcHandler
RouteData routeData = route.GetRouteData(httpContext);
if (routeData != null)
{
return routeData;
}
}
return null;
}
}
public class UrlRoutingModule : IHttpModule
{
public void Dispose()
{ } public void Init(HttpApplication context)
{
context.PostResolveRequestCache += onPostResolveRequestCache;
} public virtual void onPostResolveRequestCache(object sender, EventArgs e)
{
HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current);
RouteData routeData = RouteTable.Routes.GetRouteData(httpContext);
if (routeData == null)
{
return;
}
RequestContext requestContext = new RequestContext
{ RouteData = routeData,
HttpContext = httpContext
};
IHttpHandler handler = routeData.RouteHandler.GetHttpHandler(requestContext);//这里返回MvcHandler
httpContext.RemapHandler(handler);//UrlRoutingModule使用MvcHandler处理器处理,调用响应的processrequest方法 }
}
public class MvcRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MvcHandler(requestContext);
}
}
public class MvcHandler : IHttpHandler
{
public bool IsReusable
{
get
{
return false;
}
}
public RequestContext RequestContext { get; private set; }
public MvcHandler(RequestContext requestContext)
{
this.RequestContext = requestContext;
}
public void ProcessRequest(HttpContext context)
{
string controllerName = this.RequestContext.RouteData.Controller;
IControllerFactory controllerFactory = ControllerBuilder.Current.GetControllerFactory();
IController controller = controllerFactory.CreateController(this.RequestContext, controllerName);
controller.Execute(this.RequestContext);
}
}
public interface IController
{
void Execute(RequestContext requestContext);
}
public interface IControllerFactory
{
IController CreateController(RequestContext requestContext, string controllerName);
}
public class ControllerBuilder
{
private Func<IControllerFactory> factoryThunk;
public static ControllerBuilder Current { get; private set; }
static ControllerBuilder()
{
Current = new ControllerBuilder();
}
public IControllerFactory GetControllerFactory()
{
return factoryThunk();
}
public void SetControllerFactory(IControllerFactory controllerFactory)
{
factoryThunk = () => controllerFactory;
}
}
public class DefaultControllerFactry : IControllerFactory
{
private static List<Type> controllerTypes = new List<Type>();
/// <summary>
/// 构造函数加载所有的控制器类的类型放在controllerTypes
/// </summary>
static DefaultControllerFactry()
{
foreach (Assembly assembly in BuildManager.GetReferencedAssemblies())
{
foreach (Type type in assembly.GetTypes().Where(type => typeof(IController).IsAssignableFrom(type)))
{
controllerTypes.Add(type);
}
}
} /// <summary>
/// 通过controllerTypes集合和具体的控制器名称controllerName,反射创建对应控制器
/// </summary>
/// <param name="requestContext"></param>
/// <param name="controllerName"></param>
/// <returns></returns>
public IController CreateController(RequestContext requestContext, string controllerName)
{
string typeName = controllerName + "Controller";
Type controllerType = controllerTypes.FirstOrDefault(c => string.Compare(typeName, c.Name, true) == );
if (controllerType == null)
{
return null;
}
return (IController)Activator.CreateInstance(controllerType);
}
} public class ControllerBase : IController
{
protected IActionInvoker ActionInvoker { get; set; }
public ControllerBase()
{
this.ActionInvoker = new ControllerActionInvoker();
}
public void Execute(RequestContext requestContext)
{
ControllerContext context = new ControllerContext
{
RequestContext = requestContext,
Controller = this
};
string actionName = requestContext.RouteData.ActionName;
this.ActionInvoker.InvokeAction(context, actionName);
}
}
public interface IActionInvoker
{
void InvokeAction(ControllerContext context, string actionName);
}
/// <summary>
/// 控制器上下文:包含控制器和二次封装过的RequestContext请求上下文
/// </summary>
public class ControllerContext
{
/// <summary>
/// 包含使用的控制器
/// </summary>
public ControllerBase Controller { get; set; }
public RequestContext RequestContext { get; set; }
}
public class ControllerActionInvoker : IActionInvoker
{
public IModelBinder ModelBinder { get; private set; }
public ControllerActionInvoker()
{
this.ModelBinder = new DefaultModelBinder();
}
public void InvokeAction(ControllerContext controllerContext, string actionName)
{
MethodInfo method = controllerContext.Controller.GetType().GetMethods()
.First(m => string.Compare(actionName, m.Name, true) == );
List<object> parameters = new List<object>();
foreach (ParameterInfo parameter in method.GetParameters())
{
//把请求的参数绑定对应方法参数的模型上
parameters.Add(this.ModelBinder.BindModel(controllerContext,
parameter.Name, parameter.ParameterType));
}
ActionResult actionResult = method.Invoke(controllerContext.Controller, parameters.ToArray()) as ActionResult;
actionResult.ExecuteResult(controllerContext);
}
}
public interface IModelBinder
{
object BindModel(ControllerContext controllerContext, string modelName, Type modelType);
}
public class DefaultModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, string modelName, Type modelType)
{
if (modelType.IsValueType || typeof(string) == modelType)
{
object instance;
if (GetValueTypeInstance(controllerContext, modelName, modelType, out instance))
{
return instance;
}
return Activator.CreateInstance(modelType);
}
object modelInstance = Activator.CreateInstance(modelType);
foreach (PropertyInfo property in modelType.GetProperties())
{
if (!property.CanWrite || (!property.PropertyType.IsValueType
&& property.PropertyType != typeof(string)))
{
continue; }
object propertyValue;
if (GetValueTypeInstance(controllerContext, property.Name, property.PropertyType, out propertyValue))
{
property.SetValue(modelInstance, propertyValue, null);
}
}
return modelInstance;
}
private bool GetValueTypeInstance(ControllerContext controllerContext, string modelName, Type modelType, out object value)
{
System.Collections.Specialized.NameValueCollection form = HttpContext.Current.Request.Form;
string key;
if (null != form)
{
key = form.AllKeys.FirstOrDefault(k => string.Compare(k, modelName, true) == );
if (key != null)
{
value = Convert.ChangeType(form[key], modelType);
return true;
} }
key = controllerContext.RequestContext.RouteData.Values
.Where(item => string.Compare(item.Key, modelName, true) == )
.Select(item => item.Key).FirstOrDefault();
if (null != key)
{
value = Convert.ChangeType(controllerContext.RequestContext.RouteData.Values[key], modelType);
return true;
}
key = controllerContext.RequestContext.RouteData.DataTokens
.Where(item => string.Compare(item.Key, modelName, true) == )
.Select(item => item.Key).FirstOrDefault(); if (null != key)
{
value = Convert.ChangeType(controllerContext.RequestContext.RouteData.DataTokens[key], modelType);
return true;
}
value = null;
return false;
} }
public abstract class ActionResult
{
public abstract void ExecuteResult(ControllerContext context);
}
public class RawContentResult : ActionResult
{
public string RawData { get; private set; }
public RawContentResult(string rawData)
{
RawData = rawData;
}
public override void ExecuteResult(ControllerContext context)
{
context.RequestContext.HttpContext.Response.Write(this.RawData);
}
}
public class SimpleModel
{
public string Controller { get; set; }
public string Action { get; set; }
}
public class HomeController : ControllerBase
{
public ActionResult Index(SimpleModel model)
{
string content = string.Format("Controller:{0}<br/>Action:{1}", model.Controller, model.Action);
return new RawContentResult(content);
}
}
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
}
}
.global.asax
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState; namespace WebApp
{
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
//RouteConfig.RegisterRoutes(RouteTable.Routes);
//BundleConfig.RegisterBundles(BundleTable.Bundles); RouteTable.Routes.Add("default",
new Route { Url = "{Controller}/{Action}" }); ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactry());
}
} }
mvc模拟实现的更多相关文章
- asp.net MVC 模拟实现与源码分析
前言 本文流程#1: 从一个空项目->模拟实现一个从/Home/Test形式的URL敲入->后台逻辑处理->传入后台model参数->调用razor引擎->前台展示 涉及 ...
- Spring MVC 模拟
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- asp.net mvc 模拟百度搜索
页面代码: <td><span>*</span>车牌号码:</td> <td> <div id="search"& ...
- Asp.Net MVC<三> : ASP.NET MVC 基本原理及项目创建
MVC之前的那点事儿系列 解读ASP.NET 5 & MVC6系列 MVC模拟(摘自ASP.NET MVC5框架揭秘) Asp.net中,通过HttpModule的形式定义拦截器,也就是路由表 ...
- ASP.NET MVC 基础
ASP.NET MVC oo1 Mvc准备工作课程安排:ORM->AspNet MVC开发环境:VS2012/VS2013SqlServer2008/2005主讲Asp.Net Mvc4 Raz ...
- Spring Cloud(中文版)
原文链接:Spring Cloud I.云原生应用 Spring Cloud上下文:应用上下文服务 2.1.Bootstrap应用程序上下文 2.2.应用程序上下文层次结构 2.3.更改Bootstr ...
- AuthorizeAttribute示例
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 一、ASP.NET MVC 路由(一)--- ASP.NET WebForm路由模拟
ASP.NET WebForm 应用,用户请求的是物理文件,其中包括静态页面和动态页面,在Url中的显示都是服务器中一个物理文件的相对路径.但是ASP.NET MVC就不同了,用户请求的是Contro ...
- [转]模拟HttpContext 实现ASP.NET MVC 的单元测试
众所周知 ASP.NET MVC 的一个显著优势即可以很方便的实现单元测试,但在我们测试过程中经常要用到HttpContext,而默认情况下单元测试框架是不提供HttpContext的模拟的,本文通过 ...
随机推荐
- 解决Visual Studio “无法导入以下密钥文件”的错误
错误1无法导入以下密钥文件: Common.pfx.该密钥文件可能受密码保护.若要更正此问题,请尝试再次导入证书,或手动将证书安装到具有以下密钥容器名称的强名称 CSP: VS_KEY_ 1110Co ...
- c# 导入导出excel表格式
c#使用代码导入excel时,当遇到纯数字且大于15位时会出现编码混乱(表现为科学计数法),要想呈现与excel表中纯数字格式和在数据库中呈现纯数字,操作如下: 完成即可. 导出取决于导入的内容排版.
- three添加和移除对象
创建场景在第一章的地方就讲过怎么样创建一个最基本的场景,这里不重复了html:部分 <!doctype html><html lang="en"><h ...
- leetcode976
public class Solution { public int LargestPerimeter(int[] A) { var list = A.OrderByDescending(x => ...
- mvc框架路由原理
到目前为止已经使用过很多php框架,比如:Zendframework,ThinkPHP,YII,Slim.但还未静下心来研究过框架的原理. 今天首先来看一下mvc框架中路由的原理: 所谓路由,就是程序 ...
- Find 和 Findstr
FIND 在文件中搜索文字字符串. FINDSTR 在文件中搜索字符串. findstr能用正则表达式,而find不能 dir c:|find /N /I /C "windows&q ...
- socket 网络连接基础
socket 客户端 import socket 1.client = socket.socket() # socket.TCP/IP 选择连接的类型,默认为本地连接 2.client.connec ...
- android MD5 SHA1
参考文章: AndroidStudio 中怎样查看获取MD5和SHA1值(应用签名)(https://www.cnblogs.com/zhchoutai/p/7102516.html) 使用 java ...
- UI5-文档-4.13-Margins and Paddings
我们的应用程序内容仍然粘在信箱的角落里.要微调布局,可以向上一步添加的控件添加空白和填充. 我们将使用SAPUI5提供的标准类,而不是手工向控件添加CSS.这些类负责一致的分级步骤.从左到右的支持和响 ...
- jenkins 修改工作目录
修改Jenkins路径 Jenkins的默认安装路径是/var/lib/jenkins 现在由于这个根目录的磁盘太小,所以切换到/data 目录下. Jenkins目录.端口.工作目录等信息在/etc ...