Jan 31, 2010

ASP.NET MVC and Crystal Reports

Nearly every application nowadays needs to visualize some data presented in convenient way, with good look-and-feel, export capabilities and printing functionality.

Reports can be developed under various set of technologies and 3rd party libraries. Today we’ll take a look how to incorporate Crystal Reports with an ASP.NET MVC application.

First of all, I’d like to mention that a MVC application doesn’t support the well known Crystal Reports controls that we use in one ASP.NET web form application like – CrystalReportViewer, CrystalReportSource, Report Document, etc. That means, you cannot open a MVC view and drag these controls on it.

Fortunately we can easily find a workaround and bypass this small “limitation”.
I will review two possible solutions for integrating crystal reports as part of MVC application and will outline some pros and cons while reviewing the approaches.


A)Starting from standalone web application that incorporates crystal reports and moving it into an ASP.MVC application without creating sub application.

Creating ASP.NET forms application that incorporates crystal reports is a kid stuff and it is widely discussed over the net, so I will skip the explanations related to this small step.

The screenshot below shows a sample report that displays the access rights of our users:


Once we have the application, we may plug it into our MVC application that I have created in the one of my previous posts named MVC Custom authorization. Following this approach i can use different technologies and assemblies in my crystal report application like MS Ajax and the only one I need to do is to refer their assemblies in MVC references. And in the same time I have my Crystal Report application separately.
Technically there is only one application – the MVC one, we just take what we need from the report application and add it in MVC solution.

Next screen-shot shows the files we need in our MVC project.


Since there is only one application, in the context of the MVC application we can get rid of the web.config file. It is not really needed. To make the application compile able, exclude the folders displayed on the screen-shot.

It is very important to change the Build Action of the report file from Embedded to Content. The file will not compiled, but will be included in the Content output group.



Otherwise after deploy you will end up with no rpt file to be loaded and you will see the yellow screen of death:



And if doing so is inevitable to change the build action of the *.rpt file from embedded resource to content, when it is part of your MVC project (section B) it can easily keep his default build behavior. And I warmly advise you not to keep it and always to change it to “Content”. Otherwise you will end up doing new deploy for the sake of small change in one of your *.rpt files. And imagine what mess it could be if you have a product line in many production environments….

After compiling and running we can see that our report is displayed only when user is authenticated.





You can run your crystal project out of the context of the MVC project. Imagine that your build is broken for the next few hours? What are you going to do, waiting rather opening the solution of your report project and keep on coding?

At last, what will happen if I want to separate for some reasons (let’s say make it available to dedicated group of users) my reports as new application. In this case I will just exclude the files from my solution and touch a bit the code to adopt it for my new needs. Not a biggy…indeed it is already separate one.

B)Integrating crystal reports in MVC application via ASP.NET ASPX pages

The first thing we need to do is to add catch-all ignore route for the aspx pages, just to make sure that they won’t be handled by the MVC engine.



We can add a new folder (let’s say Reports) and define our aspx pages which host crystal reports inside. There is no really difference in doing this with creating aspx page in standalone crystal report application.



In this scenario we are not facing the problems of excluding/including new files from our inner application, but definitely our reporting part is less scalable.

You can download the Crystal Reports 2008 runtime package for .net Framework from the links, and then install it on the target machine. Don’t forget to read very carefully the license agreement.

"CRRedist2008_x86.msi" (for 32bit)
"CRRedist2008_x64.msi" (for 64bit)

http://resources.businessobjects.com/support/downloads/redistributables/vs_2008/redist/x64/CRRedist2008_x64.msi

http://resources.businessobjects.com/support/downloads/redistributables/vs_2008/redist/x86/CRRedist2008_x86.msi


Read full article!