由于.net MVC 的controller 依赖于HttpContext,而我们在上一篇中的沙箱模式已经把一次http请求转换为反射调用,并且http上下文不支持跨域,所以我们要重造一个controller。

  我们在写mvc项目的时候经常会用到ViewBag、ViewData,那我们就先声明这两个变量:

  

public dynamic ViewBag = new DynamicViewBag();
public ViewDataDictionary ViewData = new ViewDataDictionary();

  当然还可以根据自己的需要构建更多的特性。

  我们在一个网络请求中避免不了会携带一些参数,那这些参数该如何传到沙箱中呢?我们定义了一个RefRequestEntity类,他负责对我们的参数经行打包,把参数打包后对象作为参数传到沙箱内部:

/// <summary>用户的请求信息
/// </summary>
[Serializable]
public class RefRequestEntity
{
/// <summary>当前用户在本页面具备的所有权限
/// </summary>
public List<RightEntity> PageRights;
/// <summary>用户请求携带的所有参数
/// </summary>
public HuberRequest<string, object> Request;
/// <summary>
/// 用户id
/// </summary>
public string UserID { get; set; }
public RefRequestEntity()
{
PageRights = new List<RightEntity>();
Request = new HuberRequest<string, object>();
}
}

  

  在.net mvc中我们可以返回ActionResult,在ActionResult内部调用时才会做出真正的Response(更多细节请参考mvc实现原理),当然它在执行的整个过程中都是由HttpContext贯穿的,我们没有了HttpContext,我们就只自己构造一些Response方法。

  返回View()结果:

  mvc中由ViewEngine来编译执行我们写好的视图文件(.aspx、.cshtml),而我们则借助于RazorEngine来编译执行razor视图文件,它可以支持我们常用的ViewBag、using、layout等(更多请见RazorEngine)。在本篇中我们还是把精力放回controller的实现中,关于视图的实现我们在下一篇中在讲。我们先看一下一个View的简单实现:

/// <summary>返回试图的执行结果
/// </summary>
/// <returns></returns>
protected string View()
{
var tKey = Engine.Razor.GetKey(getActionPath(), ResolveType.Global);//getActionPath:获取action对应的视图文件key值。        return new CompileView().RunCompile(tKey, null, null, ViewBag); //返回执行结果
     }

  View()的执行结果是一段html代码。这样我们在请求一个action的时候,就可以正常的呈现一个页面了。下边是一个Controller基类的实现,它完成了View、PartialView的实现Demo:

public class HuberController
{ public dynamic ViewBag = new DynamicViewBag();
public ViewDataDictionary ViewData = new ViewDataDictionary(); /// <summary>设置ViewBag的值
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
internal void AddViewBageValues(string key, object value)
{ Impromptu.InvokeSet(ViewBag, key, value); } /// <summary>返回试图的执行结果
/// </summary>
/// <returns></returns>
protected string View()
{
var tKey = Engine.Razor.GetKey(getActionPath(), ResolveType.Global);
return new CompileView().RunCompile(tKey, null, null, ViewBag);
}
/// <summary>返回试图的执行结果
/// </summary>
/// <typeparam name="T">model的类型</typeparam>
/// <param name="model">model</param>
/// <returns></returns>
protected string View<T>(T model)
{
var tKey = Engine.Razor.GetKey(getActionPath(), ResolveType.Global);
return new CompileView().RunCompile(tKey, typeof(T), model, ViewBag);
}
/// <summary>返回试图的执行结果
/// </summary>
/// <param name="viewName">视图的全路径(相对于运行目录的全路径)</param>
/// <returns></returns>
protected string View(string viewName)
{
var tKey = Engine.Razor.GetKey(getActionPathWith(viewName), ResolveType.Global);
return new CompileView().RunCompile(tKey, null, null, ViewBag);
}
/// <summary>返回试图的执行结果
/// </summary>
/// <typeparam name="T">model的类型</typeparam>
/// <param name="viewName">视图的全路径(相对于运行目录的全路径)</param>
/// <param name="model">model</param>
/// <returns></returns>
protected string View<T>(string viewName, T model)
{
var tKey = Engine.Razor.GetKey(getActionPathWith(viewName), ResolveType.Global);
return new CompileView().RunCompile(tKey, typeof(T), model, ViewBag);
} /// <summary>返回局部试图的执行结果
/// </summary>
/// <returns></returns>
protected string PartialView()
{
var tKey = Engine.Razor.GetKey(getActionPath(), ResolveType.Include);
return new CompileView().RunCompile(tKey, null, null, ViewBag);
}
/// <summary>返回局部试图的执行结果
/// </summary>
/// <typeparam name="T">model的类型</typeparam>
/// <param name="model">model</param>
/// <returns></returns>
protected string PartialView<T>(T model)
{
var tKey = Engine.Razor.GetKey(getActionPath(), ResolveType.Include);
return new CompileView().RunCompile(tKey, typeof(T), model, ViewBag);
}
/// <summary>返回局部试图的执行结果
/// </summary>
/// <param name="viewName">视图的全路径(相对于运行目录的全路径)</param>
/// <returns></returns>
protected string PartialView(string viewName)
{
var tKey = Engine.Razor.GetKey(getActionPathWith(viewName), ResolveType.Include);
return new CompileView().RunCompile(tKey, null, null, ViewBag);
}
/// <summary>返回局部试图的执行结果
/// </summary>
/// <typeparam name="T">model的类型</typeparam>
/// <param name="viewName">视图的全路径(相对于运行目录的全路径)</param>
/// <param name="model">model</param>
/// <returns></returns>
protected string PartialView<T>(string viewName, T model)
{
var tKey = Engine.Razor.GetKey(getActionPathWith(viewName), ResolveType.Include);
return new CompileView().RunCompile(tKey, typeof(T), model, ViewBag);
} /// <summary>获取action对应view的物理文件地址
/// </summary>
/// <returns></returns>
private string getActionPath()
{
string key = string.Empty;
StackTrace trace = new StackTrace();
MethodBase methodName = trace.GetFrame(2).GetMethod();
string className = methodName.ReflectedType.FullName; string assName = HuberHttpModule.CurDomainAssemblyName;
key = className.Substring(assName.Length);
key = key.Replace(".Controllers.", ".Views.");
key = key.Substring(0, key.Length - 10);
key = key.Replace(".", "\\");
key += "\\" + methodName.Name + ".cshtml";
return key;
}
/// <summary>根据action名获取其对应view的物理文件地址
/// </summary>
/// <param name="ActionName">action名(同一controller中)</param>
/// <returns></returns>
private string getActionPathWith(string ActionName)
{
string key = string.Empty;
StackTrace trace = new StackTrace();
MethodBase methodName = trace.GetFrame(2).GetMethod();
string className = methodName.ReflectedType.FullName; string assName = HuberHttpModule.CurDomainAssemblyName;
key = className.Substring(assName.Length);
key = key.Replace(".Controllers.", ".Views.");
key = key.Substring(0, key.Length - 10);
key = key.Replace(".", "\\");
key += "\\" + ActionName + ".cshtml";
return key;
}
}

   我们上边列出了对Razor编译执行的简单过程,还是那句话,RazorEngine的更多实现细节将在下一篇讲解。那么现在问题来了,我们得到了html代码或者说我们执行玩自己的业务逻辑以后如何把这个结果输出呢(即HttpResponse)?

  我们定义了一个RefRespondEntity类,它来承载返回结果,并把结果返回到沙箱外层的调用者,再由这个调用者将这个RefRespondEntity对象Response出去:

  [Serializable]
public class RefRespondEntity
{
public RefRespondEntity(RespondType type)
{
ResultType = type;
}
/// <summary>返回结果的数据类型
/// </summary>
public RespondType ResultType { get; set; }
/// <summary>返回结果的内容
/// 如果是ResultType=_Redirect那么ResultContext=301
/// 如果是ResultType=_Stream那么ResultContext="文件名.zip",当然这个文件名可以随意定义
/// </summary>
public object ResultContext { get; set; }
/// <summary>返回结果的文件流
/// </summary>
public byte[] ResultStream { get; set; }
}

  

        //一个action的demo
public RefRespondEntity Index4(RefRequestEntity param)
{ object AA = param.Request["A"];
object BB = param.Request["B"];
object CC = param.Request["C"]; RefRespondEntity result = new RefRespondEntity(RespondType._String);
result.ResultContext = View();
object tt = ViewBag.test;
return result;
}

  

 

var result = sandBox.InvokeMothod(urlEntity.controller, urlEntity.action, paras);//sandBox是一个沙箱的实例化对象
RequestHandle.ResposeResult(respond, result);//输出结果

  

/// <summary>响应工具类
/// </summary>
public class RequestHandle
{
private static bool IsAjax(HttpRequest request)
{
return request.Headers["X-Requested-With"] != null;
} /// <summary>将reques请求的参数封装到CorRefEntity对象中
/// </summary>
/// <param name="para"></param>
/// <param name="request"></param>
public static void FillCorRefEntity(RefRequestEntity para, HttpRequest request)
{
foreach (var key in request.Params.AllKeys)
{
para.Request.Add(key, request.Params[key]); }
}
/// <summary>URL404
/// </summary>
/// <param name="request"></param>
/// <param name="respond"></param>
public static void ResponseNotfound(HttpRequest request, HttpResponse respond)
{
if (IsAjax(request))
{
respond.Write(ResponseCodeEntity.CODE404);
respond.End();
}
else
{
respond.Redirect(ResponseCodeEntity.ULR404);
respond.End();
}
}
/// <summary>NoLogin
/// </summary>
/// <param name="request"></param>
/// <param name="respond"></param>
public static void ResponseNoLogin(HttpRequest request, HttpResponse respond)
{
if (IsAjax(request))
{
respond.Write(ResponseCodeEntity.NoLogin);
respond.End();
}
else
{
respond.Redirect(ResponseCodeEntity.LoginURL);//需要改成非调转形式
respond.End();
}
}
/// <summary>NoRight
/// </summary>
/// <param name="request"></param>
/// <param name="respond"></param>
public static void ResponseNoRight(HttpRequest request, HttpResponse respond)
{
if (IsAjax(request))
{
respond.Write(ResponseCodeEntity.NoRight);
respond.End();
}
else
{
respond.Redirect(ResponseCodeEntity.NoRightURL);//需要改成非调转形式
respond.End();
}
} public static void ResposeResult(HttpResponse respond, object result)
{
if (typeof(RefRespondEntity) == result.GetType())
{
RefRespondEntity temp_result = (RefRespondEntity)result;
if (temp_result.ResultType == RespondType._Redirect)
{
respond.Redirect((string)temp_result.ResultContext);
respond.End();
}
else if (temp_result.ResultType == RespondType._Stream)
{
byte[] st = (byte[])temp_result.ResultStream;
respond.ContentType = "application/octet-stream";
respond.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", (string)temp_result.ResultContext));
respond.OutputStream.Write(st, 0, st.Length);
respond.End();
}
else
{
respond.Write(temp_result.ResultContext);
respond.End();
}
}
else
{
respond.Write("Huber Module respose is not a RefRespondEntity");
}
}
}

  

   public class ResponseCodeEntity
{
/// <summary>404
/// </summary>
public static string ULR404 = "/NotPageFound/_404";
/// <summary>404ajax
/// </summary>
public static string CODE404 = "NotPage";
/// <summary>登录页URL
/// </summary>
public static string LoginURL = "/User/Login";
/// <summary>未登录ajax
/// </summary>
public static string NoLogin = "NoLogin";
/// <summary>没有权限ajax
/// </summary>
public static string NoRight = "NoRight";
/// <summary>没有权限url
/// </summary>
public static string NoRightURL = "/User/NoRight";
}

  

转载请注明出处:http://www.cnblogs.com/eric-z/p/5047172.html第四篇 基于.net搭建热插拔式web框架(RazorEngine实现)

第三篇 基于.net搭建热插拔式web框架(重造Controller)的更多相关文章

  1. 第二篇 基于.net搭建热插拔式web框架(沙箱的构建)

    上周五写了一个实现原理篇,在评论中看到有朋友也遇到了我的问题,真的是有种他乡遇知己的感觉,整个系列我一定会坚持写完,并在最后把代码开源到git中.上一篇文章很多人看了以后,都表示不解,觉得不知道我到底 ...

  2. 第四篇 基于.net搭建热插拔式web框架(RazorEngine实现)

    在开头也是先给大家道个歉,由于最近准备婚事导致这篇文章耽误了许久,同时也谢谢老婆大人对我的支持. 回顾上篇文章,我们重造了一个controller,这个controller中用到了视图引擎,我们的视图 ...

  3. 第五篇 基于.net搭建热插拔式web框架(拦截器---请求管道)

    好了,前边我们把核心内容介绍完了,接下来要做的就是拦截用户的请求,并把请求转向沙箱内. 这里我们准备通过实现一个HttpModule类来完成请求的拦截与转发.新建一个HuberHttpModule类, ...

  4. 基于.net搭建热插拔式web框架(实现原理)

    第一节:我们为什么需要一个热插拔式的web框架? 模块之间独立开发 假设我们要做一个后台管理系统,其中包括“用户活跃度”.“产品管理”."账单管理"等模块.每个模块中有自己的业务特 ...

  5. net搭建热插拔式web框架

    net搭建热插拔式web框架(重造Controller) 由于.net MVC 的controller 依赖于HttpContext,而我们在上一篇中的沙箱模式已经把一次http请求转换为反射调用,并 ...

  6. net搭建热插拔式web框架(沙箱的构建)

    net搭建热插拔式web框架(沙箱的构建) 上周五写了一个实现原理篇,在评论中看到有朋友也遇到了我的问题,真的是有种他乡遇知己的感觉,整个系列我一定会坚持写完,并在最后把代码开源到git中.上一篇文章 ...

  7. 带你手写基于 Spring 的可插拔式 RPC 框架(一)介绍

    概述 首先这篇文章是要带大家来实现一个框架,听到框架大家可能会觉得非常高大上,其实这和我们平时写业务员代码没什么区别,但是框架是要给别人使用的,所以我们要换位思考,怎么才能让别人用着舒服,怎么样才能让 ...

  8. 转-基于NodeJS的14款Web框架

    基于NodeJS的14款Web框架 2014-10-16 23:28 作者: NodeJSNet 来源: 本站 浏览: 1,399 次阅读 我要评论暂无评论 字号: 大 中 小 摘要: 在几年的时间里 ...

  9. 2、基于wsgiref模块DIY一个web框架

    一 web框架 Web框架(Web framework)是一种开发框架,用来支持动态网站.网络应用和网络服务的开发.这大多数的web框架提供了一套开发和部署网站的方式,也为web行为提供了一套通用的方 ...

随机推荐

  1. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  2. 关于AngularJS(1)

      在讲正题之前,先说一下有关angular简介方面的信息: 1. angularJS  诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经 ...

  3. .NET跨平台之旅:成功将示例站点升级至ASP.NET Core RC2

    ASP.NET Core RC2 终于发布了( Announcing ASP.NET Core RC2 ).为了庆祝这次发布,我们将运行在 Ubuntu 服务器上的示例站点 about.cnblogs ...

  4. 学券制(教育券、school voucher)

    美国「学券制」是怎样的一种制度?它为什么是共和党的执政政策?它在美国及其它地区有实施吗?效果如何?能否在保证公平的同时,通过市场提高教育质量? 作者:冉筱韬链接:https://www.zhihu.c ...

  5. C++11中的std::function

    看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::fun ...

  6. vs2013给类添加默认注释

    目录:D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\20 ...

  7. ceph hadoop spark 大数据处理

    http://docs.ceph.com/docs/giant/cephfs/hadoop/ https://indico.cern.ch/event/524549/contributions/218 ...

  8. java之并发编程线程池的学习

    如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间. java.uitl.concurrent.Thre ...

  9. mybatis判断传入list大小

    <if test="tenantIds.size() > 0"> AND A.PROC_TARGET_ID IN <foreach collection=& ...

  10. 篇二:MySQL存储过程

    目的:写一个存储过程,往数据库中插入几百条数据,作为识别码给别人用(这里我觉得和验证码的功能相似) BEGIN ); ); ) ; ); ); ; ; while count <= insert ...