渲染视图为一个字符串在ASP.NET MVC 2 [英] Rendering a view to a string in ASP.NET MVC 2

查看:182
本文介绍了渲染视图为一个字符串在ASP.NET MVC 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要渲染的的ActionResult 字符串来的页面添加到我们的内部搜索引擎索引。我们选定了这个解决方案呈现给字符串

We need to render an ActionResult to a string to add pages to our internal search engine index. We settled on this solution to render to string.

我碰到的一个问题的ExecuteReuslt 通话用来处理视图。

I've run into a problem with the ExecuteResult call used to process the View.

var oldController = controllerContext.RouteData.Values["controller"];
controllerContext.RouteData.Values["controller"] = 
                  typeof(TController).Name.Replace("Controller", "");

viewResult.ExecuteResult(controllerContext); // this line breaks

我收到以下错误:

I receive the following error:

对象未设置为对象的实例错误。

我已经证实了的ViewResult 不为空,所以异常必须在的ExecuteReuslt 内部抛出。

I've confirmed viewResult is not null, so the exception has to be thrown internally in ExecuteResult.

什么,我们还缺?

推荐答案

我想它了。与该的ExecuteReuslt问题是工作不正常,是因为我没有嘲笑ControllerContext正常。我能够在这个SO职位描述嘲讽controllercontext来解决这一问题:

I figured it out. The issue with ExecuteResult wasn't working as expected was because I hadn't mocked the ControllerContext properly. I was able to solve the issue by mocking the controllercontext as described in this SO post:

<一个href="http://stackoverflow.com/questions/32640/mocking-asp-net-mvc-controller-context">http://stackoverflow.com/questions/32640/mocking-asp-net-mvc-controller-context

希望这篇文章可以帮助别人,将来如果他们试图做类似的事情。

Hope this post helps someone in the future if they're trying to do something similar.

这篇关于渲染视图为一个字符串在ASP.NET MVC 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆