博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC原理
阅读量:5949 次
发布时间:2019-06-19

本文共 4841 字,大约阅读时间需要 16 分钟。

调用流程,不多阐述,请看下图

 

下面通过代码执行流程演示

 

1.浏览器请求:http://localhost/SimpleMvc/Home/Index

2.在Global.asax文件的Application_Start方法中注册路由服务

protected void Application_Start(object sender, EventArgs e)        {            RouteTable.Routes.Add("default", new Route("{controller}/{action}",new SimpleRouteHandler()));        } controller\action会直接映射为路由表中的keyValue键值,方便下一步路

3. 1的url会通过路由表进入IRouteHandler(自定义SimpleRouteHandler

public class SimpleRouteHandler:IRouteHandler    {        public IHttpHandler GetHttpHandler(RequestContext requestContext)        {            return new SimpleHttpHandler(requestContext.RouteData.Values);        }    } 这里只是演示mvc的原理,因此只将路由相关的数据传递下去,实际场景中还有很多其他的参数需要映射和传

4.参考上面的实现,我们这里只把路由数据传递给IHttpHandler(自定义SimpleHttpHandler

public class SimpleHttpHandler:IHttpHandler    {        public SimpleHttpHandler(RouteValueDictionary routeData)        {            _routeData = routeData;        }        private RouteValueDictionary _routeData;        public bool IsReusable        {            get { throw new NotImplementedException(); }        }        ///         /// 通过controller,action激活Controller,然后执行Action,返回结果        ///         ///         public void ProcessRequest(HttpContext context)        {            var model = new SimpleModle();             model.Controller =_routeData["controller"].ToString();             model.Action =_routeData["action"].ToString();                       var control = ControllerFactory.Instance.GetController(model.Controller);            if (control!=null)            {                var view = control.Excute(model.Action, new object[] { model });                if (view!=null)                {                    context.Response.Write(view);                    return;                }            }            context.Response.Write("

不支持该请求

"); } }

4.1如何激活controller可以参考以下代码

public class ControllerFactory    {        private ControllerFactory()        {        }        static ControllerFactory()        {        }        private const string END = "Controller";        private static object syn = new object();        private Dictionary
_contrDc = new Dictionary
(); public static readonly ControllerFactory Instance = new ControllerFactory(); ///
/// 根据controller名称返回实例 /// ///
///
public IController GetController(string controllerName) { if (!string.IsNullOrEmpty(controllerName)) { if (!controllerName.EndsWith(END)) { controllerName += END; } if (!_contrDc.ContainsKey(controllerName)) { lock (syn) { if (!_contrDc.ContainsKey(controllerName)) { var type = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(s => typeof(IController).IsAssignableFrom(s) && string.Equals(s.Name, controllerName, StringComparison.InvariantCultureIgnoreCase)); _contrDc.Add(controllerName, type); } } } if (_contrDc[controllerName] != null) { return (IController)Activator.CreateInstance(_contrDc[controllerName]); } } return null; } }

4.2,controller如何执行action,可以参考一下代码

public interface IController    {        object Excute(string action, object[] parameters);    }    public abstract class ControllerBase : IController    {        public virtual object Excute(string action,object[] parameters)        {            var method = this.GetType().GetMethod(action);            if (method!=null)            {               return method.Invoke(this, parameters);            }            return null;        }    }    public class HomeController : IController    {        public object Excute(string action, object[] parameters)        {            var model = parameters[0] as SimpleModle;            string content = "参数无效";            if (model != null)            {                content = string.Format("Controller:{0}
Action:{1}", model.Controller, model.Action); } return new RawContentResult(content); } }

4.3 简单的Modle

public class SimpleModle    {        public string Controller { get; set; }        public string Action { get; set; }    }

4.4 简单的view

public abstract class ActionResult    {    }  class RawContentResult : ActionResult    {        public RawContentResult(string content)        {            Content = content;        }        public string Content { get; set; }        public override string ToString()        {            return Content;        }

 

5.返回结果:

转载于:https://www.cnblogs.com/lzzhang/p/4992205.html

你可能感兴趣的文章
YOUTH--- Samuel Ullman
查看>>
SCCM2012日志文件解释
查看>>
nodejs学习笔记-1
查看>>
nagios安装笔记(仅安装无配置)
查看>>
Windows批处理学习(二)——批处理(3)
查看>>
敏捷开发般若敏捷系列之五:如何推广敏捷(中)(无寿者,回报,破我执)...
查看>>
邱县祥云网站上线了
查看>>
mysql优化和索引
查看>>
大学生暑期实践活动---关注少数民族孤寡老人
查看>>
linux学习之查看程序端口占用情况
查看>>
linux下配置安装OpenJDK+Tomcat
查看>>
相逢在栀枝花开的季节
查看>>
Ajax实现直链(点击量统计)
查看>>
linux下git自动补全命令
查看>>
Ubuntu14.04LTS更新源
查看>>
Ubuntu上hi3531交叉编译环境arm-hisiv100nptl-linux搭建过程
查看>>
oracle分区表、分区索引
查看>>
Tomcat+memcached+Nginx实现session共享
查看>>
Array operation
查看>>
Python基础学习三 文件操作(一)
查看>>