[英]Rendering a view to a string in ASP.NET MVC 2
We need to render an ActionResult
to a string
to add pages to our internal search engine index. 我们需要将
ActionResult
呈现给string
以将页面添加到我们的内部搜索引擎索引中。 We settled on this solution to render to string . 我们决定将此解决方案渲染为字符串 。
I've run into a problem with the ExecuteResult
call used to process the View. 我遇到了用于处理View的
ExecuteResult
调用的问题。
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: 我收到以下错误:
"Object reference not set to instance of object" error.
I've confirmed viewResult
is not null, so the exception has to be thrown internally in ExecuteResult
. 我已经确认
viewResult
不为null,因此必须在ExecuteResult
内部抛出异常。
What could we be missing? 我们还能错过什么?
I figured it out. 我想到了。 The issue with ExecuteResult wasn't working as expected was because I hadn't mocked the ControllerContext properly.
ExecuteResult的问题没有按预期工作,因为我没有正确地模拟ControllerContext。 I was able to solve the issue by mocking the controllercontext as described in this SO post:
我能够通过模拟controllerContext来解决这个问题,如本SO帖子中所述:
Mocking Asp.net-mvc Controller Context 模拟Asp.net-mvc控制器上下文
Hope this post helps someone in the future if they're trying to do something similar. 希望这篇文章能帮助将来的某个人,如果他们想要做类似的事情。
我自己没有尝试过,但Phil Haack确实提出了一种方法,在这里输入一个视图到字符串 MVC。
MVCContrib有一个BlockRenderer类 ,可以对这种情况有所帮助。
不是你的问题的直接答案,但为什么你将整个视图作为字符串呈现给你的搜索索引,而不是a)将数据本身推送到索引(LINQ to Lucene允许你这样做w /属性)或b )将工作卸载到一个单独的进程,该进程使用一组权限来爬行前端?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.