How to render Active Reports WebViewer in ASP.NET MVC

对着背影说爱祢 提交于 2019-11-27 10:37:54

问题


I asked this question in the DataDynamics forum earlier today. I thought that maybe I'd get some sort of response here at SO.

I am trying to get the WebViewer up and running in my ASP.NET MVC application. I am attempting to render the webviewer in the controller (webViewer.RenderControl(htmlTextWriter) and then put the results into ViewData and display the report in my view. I dont' even know if this is the correct way to go about this. Any help would be greatly appreciated.

Controller code:

    public ActionResult Display()
    {

        CurrentReport = new DetailedReport { ReportData = new DetailedData() { Repository = _repository } };

        var webViewer = new WebViewer();
        CurrentReport.Run();
        webViewer.ID = "WebViewer1";
        webViewer.Visible = true;
        webViewer.ViewerType = ViewerType.HtmlViewer;
        webViewer.Width = Unit.Percentage(100);
        webViewer.Report = CurrentReport;


        var stringWriter = new StringWriter();
        var htmlTextWriter = new HtmlTextWriter(stringWriter);
        webViewer.RenderBeginTag(htmlTextWriter);
        webViewer.RenderControl(htmlTextWriter);
        webViewer.RenderEndTag(htmlTextWriter);

        ViewData["WebViewer"] = stringWriter.ToString();

        return View();
    }

Display.aspx code:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Register assembly="ActiveReports.Web, Version=5.2.1013.2, Culture=neutral, PublicKeyToken=cc4967777c49a3ff" namespace="DataDynamics.ActiveReports.Web" tagprefix="ActiveReportsWeb" %>
<%@ Import Namespace="xxxx.Core"%>

<asp:Content ID="Content1" ContentPlaceHolderID="ClientAdminContent" runat="server">
    <%=ViewData["WebViewer"] %>
</asp:Content>

Error:

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 60: var htmlTextWriter = new HtmlTextWriter(stringWriter); Line 61: webViewer.RenderBeginTag(htmlTextWriter); Line 62: webViewer.RenderControl(htmlTextWriter); Line 63: webViewer.RenderEndTag(htmlTextWriter); Line 64:

Source File: C:\Projects\xxxx\xxxx\app\xxxx.Web.Controllers\ReportsController.cs Line: 62

****Update:****

Based on the answer by scott (thank you) my controller now looks like this:

    public ActionResult Display()
    {
        ViewData["Report"] = new DetailedReport { ReportData = new DetailedReport { ReportData = new DetailedData() { Repository = _repository } };
        return View();
    }

And my view looks like this: (I have no code behind files for my views).

<%
    var report = (ActiveReport3) ViewData["Report"];
    report.Run();
    WebViewer1.Report = report;
%>
<ActiveReportsWeb:WebViewer ID="WebViewer1" runat="server" Height="100%" Width="100%" ViewerType="AcrobatReader" />

I watch it go through debugger, and it seems to correctly step through the Details section, putting values into my fields. But after all is done, I get the message "No Report Specified." I'm hoping that I really don't have to use a codebehind file on my view because I'm not using them anywhere else. I have also debugged to verify that report.Document.Pages.Count > 0. I have put the code block both above and below the WebViewer control (don't think that really matters). Any additional thoughts?

****Update #2:****

I ended up using the answer found here: Alternative to using the OnLoad event in an ASP.Net MVC View? in combination with scott's excellent answer below. It was a timing thing with generating and binding the report to the control. So my View looks like this in the end... (where Model.Report is an ActiveReport3)

<script runat="server">
    private void Page_Load(object sender, EventArgs e)
    {
        var report = Model.Report;
        report.Run();
        WebViewer1.Report = report;
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ClientAdminContent" runat="server">
    <ActiveReportsWeb:WebViewer ID="WebViewer1" runat="server" Height="100%" Width="100%" ViewerType="AcrobatReader" />
</asp:Content>

Thanks for everyone's help!


回答1:


We have investigated this internally and found the following solution. You can add the WebViewer to a View normally. There is no need for the complicated low-level interaction code in your example. Instead, just add the WebViewer to your aspx view normally. In our sample the WebViewer was added as follows:

<ActiveReportsWeb:WebViewer ID="WebViewer1" runat="server" Height="100%" Width="100%" ViewerType="AcrobatReader" />

That is enough to get the WebViewer working on the view.

In the controller we specified an ActiveReport as follows:

ViewData["Report"] = new SampleReport();

In the codebehind of the View we hook the report to the view:

WebViewer1.Report = ViewData["Report"] as ActiveReport3;

Then the tricky part begins. There are some IHttpHandlers used by ActiveReports when running under ASP.NET for some of the viewer types such as AcrobatReader / PDF. To ensure our handlers are working you must get ASP.NET MVC routing to allow them to process as normal. Luckily it is easy to do so. Just add the following line of code to the Global.asax.cs file:

routes.IgnoreRoute("{*allarcachitems}", new { allarcachitems = @".*\.ArCacheItem(/.*)?" });

That will ignore the route. Note that according to my reading there may be problems since ASP.NET routing seems to allow only a single "catch all" route like this. Therefore, if you have multiple of these IgnoreRoute commands and or you have any problems with a .axd file, you'll need to modify the constraints dictionary argument to accomidate the .axd as well as .ArCacheItem.

For more information see the following article: http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx

You can download the full sample from our forums at http://www.datadynamics.com/forums/ShowPost.aspx?PostID=121907#121907

Scott Willeke
Data Dynamics / GrapeCity




回答2:


For anyone else having this issue and using IIS7 Make sure you add the Active reports handlers under the <handlers> section not <httpHandlers>.

<add name="RPX" verb="*" path="*.rpx" type="DataDynamics.ActiveReports.Web.Handlers.RpxHandler, ActiveReports.Web, Version=6.1.2814.0, Culture=neutral, PublicKeyToken=cc4967777c49a3ff"/>
            <add name="ActiveReport" verb="*" path="*.ActiveReport" type="DataDynamics.ActiveReports.Web.Handlers.CompiledReportHandler, ActiveReports.Web, Version=6.1.2814.0, Culture=neutral, PublicKeyToken=cc4967777c49a3ff"/>
            <add name="ArCacheItem" verb="*" path="*.ArCacheItem" type="DataDynamics.ActiveReports.Web.Handlers.WebCacheAccessHandler, ActiveReports.Web, Version=6.1.2814.0, Culture=neutral, PublicKeyToken=cc4967777c49a3ff"/>

I thought i had the IgnoreRoute setup improperly because I was getting 404 errors. However I was following the tutorial given by ActiveReports which has them in the IIS6 section instead of IIS7.



来源:https://stackoverflow.com/questions/668328/how-to-render-active-reports-webviewer-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!