让 ASP.NET MVC 支持 HotSwap

news/2024/7/10 22:13:50 标签: ASP.net, .net, ASP, MVC, Web
<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/46860.html" frameborder="0" width="468" scrolling="no" height="60"></iframe>
导读:

  

  在HowToStart那帖中,我提到了ASP.NET MVC的问题 :

  如果修改一次Controller的代码,就导致ASP.NET重启一次 , 那么会不会很麻烦?

  有时候一个项目大一点, ASP.NET启动一次需要几十秒, 那样的等待真的很浪费时间.

  与思归的交谈中, 他提到了HotSwap的概念. "热插拔" - 编写代码后不需要重新编译或重启ASP.NET.

  怎样实现这个功能呢? 现在MVC的文档很少. 只能通过Reflector去找答案了.

  一个Request,从Url, 一直到了具体的 Controller , 经过了那些步骤呢?

  下面列出了这个步骤的详细:

  1. Url

  2. RouteTable

  3. MvcRouteHandler

  4. MvcHandler

  5. ControllerBuilder

  6. ControllerFactory

  7. Controller

  这个过程中, MvcRouteHandler 是连接 RouteTable 和 MvcHandler 的桥梁.

  MvcHandler则根据传递过来的信息, 根据某种规则, 从所有的Assembly中找到对应的Controller类

  然后把类型信息传递给 ControllerBuilder/ControllerFactory , 用于指定一个更具体的Controller.

  (类似RuntimeEntity那篇文章说的子类化模式..)

  看来, 需要对MvcHandler下手了. 而 MvcRouteHandler 则就是 MvcHandler 的 Factory.

  所以同时也要编写MvcRouteHandler的实现 :

  public class HotSwapMvcRouteHandler : System.Web.Mvc.MvcRouteHandler

  {

  protected override IHttpHandler GetHttpHandler(System.Web.Mvc.RequestContext requestContext)

  {

  HotSwapMvcHandler handler = new HotSwapMvcHandler();

  handler.RequestContext = requestContext;

  return handler;

  }

  }

  public class HotSwapMvcHandler : System.Web.Mvc.MvcHandler

  {

  protected override Type GetControllerType(string controller)

  {

  string name = controller + "Controller";

  string file = "~/Controllers/" + name + ".cs";

  if (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(file)))

  return base.GetControllerType(controller);

  System.Reflection.Assembly assembly = System.Web.Compilation.BuildManager.GetCompiledAssembly(file);

  return assembly.GetType(name, true, true);

  }

  }

  上面2个类, 写在App_Code里就OK了, 然后, 需要修改一下 RouteTable 的写法:

  RouteTable.Routes.Add(new Route

  {

  Url = "[controller]/[action]/[id]",

  Defaults = new { action = "Index", id = "" },

  RouteHandler = typeof(HotSwapMvcRouteHandler)

  });

  注意一下 , RouteHandler由MvcRouteHandler替换成 HotSwapMvcRouteHandler 了.

  这样HotSwapMvcRouteHandler的代码就能够被执行, 返回HotSwapMvcHandler

  HotSwapMvcHandler 这里得到了一个 controller 的参数, 就是它了.

  我们要做的事情,就是根据它 , 找到我们想要的 Controller

  上面的GetControllerType的具体实现也实在太简单了.

  如果存在 ~/Controllers/XxxControler.cs

  就编译它, 然后直接返回 XxxController 类型.

  否则就按传统的方式去寻找对应的类.

  ----

  OK , 就这样2步, 就实现了在 ~/Controllers/ 中存放Controller代码的目的了.

  重温一下这2步:

  1. App_Code中增加 HotSwapMvcRouteHandler , HotSwapMvcHandler

  2. Global.asax中修改RouteTable的定义

  然后,就可以在 ~/Controllers/ 下增加 TestController.cs , 内容是

  public class TestController : Controller

  {

  [ControllerAction]

  public void Index()

  {

  ViewData["typename"] = this.GetType().AssemblyQualifiedName;

  ViewData["version"] = "version5";

  RenderView("index");

  }

  }

  然后创建对应的 ~/Views/Test/index.aspx 把东西显示出来:

  index.aspx

  


  

  


  

  


  

  显示:

  version5

  --------------------------------------------------------------------------------

  TestController, App_Web_testcontroller.cs.39ee6e8a.c0bebgfm, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

  --------------------------------------------------------------------------------

  111

  不断地刷新它, Application["mystr"]就会越来越长.

  然后去修改 TestController.cs 把. 把ViewData["version"] = "version123";

  那么就会立刻生效, 显示version123,而Application["mystr"]没有重置, 证明ASP.NET并没有重启.

  想提高开发的方便性的朋友 , 不妨试试上面的代码.



本文转自

http://blog.joycode.com/lostinet/archive/2007/12/11/112496.aspx

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1929196



http://www.niftyadmin.cn/n/598580.html

相关文章

Jquery对于input事件的处理

Jquery对于input事件的处理。获取此表单元素&#xff0c;以及其内容 .text()不能使用在表单选项上 要使用.val&#xff08;&#xff09;input元素使用类似onblur(this),事件在function(obj)中$(this)不能获得此元素为jquery对象&#xff0c;需要使用id选择器找到此元素转载于:h…

Http请求处理流程

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/46860.html" frameborder"0" width"468" scrolling"no" height"60"></iframe>导读&#xff1…

cartographer配置

出现下面问题 [cartographer_node-7] process has died [pid 29739, exit code -6, cmd /home/exbot/catkin_ws/devel/lib/cartographer_ros/cartographer_node -configuration_directory /home/exbot/catkin_ws/src/cartographer_ros/cartographer_ros/configuration_files -…

ubuntu 命令

find /usr -iname logging.h gnome-search-tool//搜索文件夹里的内容

WPS如何并排放置两张图片_集合30项图片处理功能,这款工具到底多牛?

最近这些年随着互联网和智能手机技术的快速发展&#xff0c;我们的工作和生活也在潜移默化中发生了巨大的改变。尤其是手机摄像头以及图片处理技术的发展&#xff0c;不仅让拍照变得更加有趣&#xff0c;也极大的方便了我们的生活。很多老铁会认为拍照就是个娱乐方式嘛&#xf…

ubuntu下qt安装、及使用

QtCreator安装 QtCreator安装方式很多&#xff0c;本文以Qt 5.2.0为例&#xff0c;我直接下载离线安装程序安装。 下载安装 从http://qt-project.org/downloads下载QtCreator安装程序。对于Ubuntu 32位系统&#xff0c;点击 Qt 5.2.0 for Linux 32-bit (425 MB) 将下载离线安…

java中灵活配置简单易用功能强大的MybatisPlus基础CRUD增删改查代码一键生成器的工具类

首先导入相关maven依赖 <!-- 安装mybatisplus --> <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version> </dependency> <!-- 安装mybatisplu…

HttpModule 介绍

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/46860.html" frameborder"0" width"468" scrolling"no" height"60"></iframe>导读&#xff1…