3
我们有一个可插入的框架,返回ActionResult
将事物呈现给浏览器的对象。最新的要求是我们的插件应该可以从常规的ASP.NET Web Forms应用程序中调用。如何使用MVC Action来自常规Web表单(伪造ControllerContext)?
到目前为止,我想出了这一点,这适用于非常基本的ActionResults:
public class ActionResultTranslator {
HttpContextBase _context;
public ActionResultTranslator(HttpContextBase context) {
_context = context;
}
public void Execute(ActionResult actionResult) {
ControllerContext fakeContext = new ControllerContext();
fakeContext.HttpContext = _context;
actionResult.ExecuteResult(fakeContext);
}
}
您将上述调用从web形式:
protected void Page_Load(object sender, EventArgs e) {
HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);
var translator = new ActionResultTranslator(contextWrapper);
translator.Execute(new RedirectResult("http://google.com"));
}
还有什么我需要做什么来勾住一切?例如,如果我想返回一个ViewResult呢?
如果我没有“动作”和“控制器”提供什么?如果我的插件返回一个ViewResult,我可以以某种方式模拟MVC管道,以便我可以找到视图并呈现结果,以便将其流式传输到浏览器中?或者我可以在我的Web窗体项目中添加一个“虚拟”控制器类和一个View文件夹? – intoOrbit 2009-10-30 16:15:12
'controller'和'action'值只是告诉视图引擎在哪里看的字符串,所以他们不必引用真正的控制器类或操作方法。实际上,它们只是您要呈现的.aspx的文件夹和文件名,只要您的项目中有一个'/ Views/controller/action.aspx'文件,就应该没问题。 – stevemegson 2009-10-30 19:07:09