首先,一些背景:
- 语言 - C#
- 平台 - .Net Framework 4.5
- 项目类型 - ASP.Net MVC 4
我正在尝试确定 MVC 项目中的哪个视图正在处理对以下方法的显式调用。该方法的 MSDN 文档在这里:http: //msdn.microsoft.com/EN-US/library/dd492930.aspx
protected internal ViewResult View(
Object model
)
原作者使用视图生成带有第三方库的 PDF 文件。我需要修改视图以包含其他信息。
问题: 我无法找到要修改的视图。他们有数百个,而且(恕我直言)他们的名字和组织都很差。生成 PDF 的基本过程如下所示。我在第 3 步和第 4 步之间感到困惑。
- 将实体的 ID 传递给 ActionResult
- 从后备存储中检索实体
- 模型传递给上面提到的Controller.View方法:
var viewModel = View(model) ;
var xmlText = RenderActionResultToString( viewModel ); - 生成的 ViewResult 与 ControllerContext 的实例一起使用以生成 HTML,就像浏览器请求一样。
- 生成的 HTML 将传递给第三方工具并转换为 PDF。
我非常清楚地了解其他一切。我不明白的是,对View(model)的调用如何确定在返回 ViewResult 时要使用哪个 View 文件。非常感谢任何帮助!
我包括下面的代码,以防它帮助任何人确定答案。
行动结果:
public ActionResult ProposalPDF(String id, String location, bool hidePrices = false)
{
var proposal = _adc.Proposal.GetByKey(int.Parse(id));
var opportunity = _adc.Opportunity.GetByKey(proposal.FkOpportunityId.Value);
ViewData["AccountId"] = opportunity.FkAccountId;
ViewData["AccountType"] = opportunity.FkAccount.FkAccountTypeId;
ViewData["Location"] = location;
ViewData["HidePrices"] = hidePrices;
return ViewPdf(proposal);
}
ViewPDF 方法:
protected ActionResult ViewPdf(object model)
{
// Create the iTextSharp document.
var document = new Document(PageSize.LETTER);
// Set the document to write to memory.
var memoryStream = new MemoryStream();
var pdfWriter = PdfWriter.GetInstance(document, memoryStream);
pdfWriter.CloseStream = false;
document.Open();
// Render the view xml to a string, then parse that string into an XML dom.
var viewModel = View(model);
var xmlText = RenderActionResultToString(viewModel);
var htmlPipelineContext = new HtmlPipelineContext();
htmlPipelineContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
//CSS stuff
var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
var cssResolverPipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlPipelineContext, new PdfWriterPipeline(document, pdfWriter)));
var xmlWorker = new XMLWorker(cssResolverPipeline, true);
var xmlParser = new XMLParser(xmlWorker);
xmlParser.Parse(new StringReader(xmlText));
// Close and get the resulted binary data.
document.Close();
var buffer = new byte[memoryStream.Position];
memoryStream.Position = 0;
memoryStream.Read(buffer, 0, buffer.Length);
// Send the binary data to the browser.
return new BinaryContentResult(buffer, "application/pdf");
}
RenderActionResultToString 辅助方法:
protected string RenderActionResultToString(ActionResult result)
{
// Create memory writer.
var sb = new StringBuilder();
var memWriter = new StringWriter(sb);
// Create fake http context to render the view.
var fakeResponse = new HttpResponse(memWriter);
var fakeContext = new HttpContext(System.Web.HttpContext.Current.Request, fakeResponse);
var fakeControllerContext = new ControllerContext(new HttpContextWrapper(fakeContext), this.ControllerContext.RouteData, this.ControllerContext.Controller);
var oldContext = System.Web.HttpContext.Current;
System.Web.HttpContext.Current = fakeContext;
// Render the view.
result.ExecuteResult(fakeControllerContext);
// Restore data.
System.Web.HttpContext.Current = oldContext;
// Flush memory and return output.
memWriter.Flush();
return sb.ToString();
}