One of the new features of the first version of Stimulsoft Reports and Dashboards 2023 is support for working with Razor Pages in Reports.WEB and Dashboards.WEB products. In this article, we will tell you more about it.

What are Razor Pages?

The Razor Pages functionality appeared on the ASP.NET Core platform back in 2019. Essentially, a Razor Page is a page that consists of standard markup and a backend class that serves as both a model and a controller. This feature allows you to create pages with Razor code and handle requests. In other words, Razor Pages are a close analog to web forms and represent an alternative solution for creating applications, primarily small ones.

What has changed?

For our ASP.NET Core components included in the Reports.WEB and Dashboards.WEB products, we added the ability to create web applications and projects using Razor Pages. In the created projects, all tools for creating, displaying, and transforming reports and dashboards will be available. All existing options and settings for components are available, as well as the ability to build and export reports using server-side code.

How it works?

Before the version 2023.1, our ASP.NET Core components worked only with MVC technology, which naturally has its pros and cons. For comparison, to display a component on a web page using MVC, it was necessary to define the component markup and its options on the View, then define all necessary actions in the Controller, and if necessary, define the data structure in the Model:

View:
@Html.StiNetCoreViewer(new StiNetCoreViewerOptions()
{
	Actions =
	{
		GetReport = "GetReport",
		ViewerEvent = "ViewerEvent"
	}
})

Controller:
public IActionResult GetReport()
{
	var report = new StiReport();
	report.LoadDocument(StiNetCoreHelper.MapPath(this, "Reports/SimpleList.mdc"));

	return StiNetCoreViewer.GetReportResult(this, report);
}

public IActionResult ViewerEvent()
{
	return StiNetCoreViewer.ViewerEventResult(this);
}

When using Razor Pages technology, all code can be placed on a single page, and events for GET and POST requests are defined instead of controller actions. For example, the same code for deploying our component looks like this:
@Html.StiNetCoreViewer(new StiNetCoreViewerOptions()
{
	Actions =
	{
		GetReport = "GetReport",
		ViewerEvent = "ViewerEvent"
	}
})

public IActionResult OnPostGetReport()
{
	var report = new StiReport();
	report.LoadDocument(StiNetCoreHelper.MapPath(this, "Reports/SimpleList.mdc"));

	return StiNetCoreViewer.GetReportResult(this, report);
}

public IActionResult OnGetViewerEvent()
{
	return StiNetCoreViewer.ViewerEventResult(this);
}

public IActionResult OnPostViewerEvent()
{
	return StiNetCoreViewer.ViewerEventResult(this);
}

The code for Razor Pages has a very similar structure, but there are some differences:

  • Instead of actions, request handling events are used;
  • Instead of a controller, the page object is passed to the resulting functions;
  • The name of the event is the standard prefix of the request type, either OnGet or OnPost, and the event name set in the component's settings.

Like with the MVC technology, the resulting function returns the required response, depending on the required event of the viewer. Therefore, in general, the component's functionality remains unchanged, and it supports both technologies equally.

Additional Information

We have tried to simplify the deployment of the component as much as possible and maintain compatibility with previous versions, so no significant code changes are required to switch to Razor Pages. We have updated the documentation and added many samples. We hope that all of this will significantly simplify the integration of components into your application and, in the case of switching from MVC to Razor Pages, will not cause any difficulties.
If you have any questions, contact us.
By using this website, you agree to the use of cookies for analytics and personalized content. Cookies store useful information on your computer to help us improve efficiency and usability. For more information, please read the privacy policy and cookie policy.