This example incorporates the Web Document Viewer into a client-side app built with Vue. The example consists of two parts:
- The ServerSideApp folder contains the backend project. The project is an ASP.NET Core application that enables cross-domain requests (CORS) (Access-Control-Allow-Origin) and implements custom web report storage.
- The vue-document-viewer folder contains the client application built with Vue.
Quick Start
Server
In the ServerSideApp folder, run the following command:
Codedotnet run
The server starts at http://localhost:5000. To debug the server, run the application in Visual Studio.
Client
In the vue-document-viewer folder, run the following commands:
Codenpm install
npm run dev
Open your browser and navigate to the URL specified in the command output to see the result.The application displays the Web Document Viewer.
Files to Review
Documentation
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
Code<template>
<div ref="viewer" style="position: absolute; left: 0; right: 0; top: 0; bottom: 0"></div>
</template>
<script>
import 'devexpress-reporting/dx-webdocumentviewer';
import {DxReportViewer} from 'devexpress-reporting/dx-webdocumentviewer';
import * as ko from 'knockout';
export default {
name: "WebDocumentViewer",
mounted() {
const reportUrl = ko.observable("TestReport");
const viewerRef = this.$refs.viewer;
const requestOptions = {
host: " http://localhost:5000/",
invokeAction: "DXXRDV"
};
const viewer = new DxReportViewer(viewerRef, {
reportUrl,
requestOptions
});
viewer.render();
},
beforeUnmount() {
ko.cleanNode(this.$refs.viewer);
}
};
</script>
JavaScriptimport "devextreme/dist/css/dx.light.css";
import "@devexpress/analytics-core/dist/css/dx-analytics.common.css";
import "@devexpress/analytics-core/dist/css/dx-analytics.light.css";
import "devexpress-reporting/dist/css/dx-webdocumentviewer.css";
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
C#using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace ServerSideApp.Controllers {
public class HomeController : Controller {
public IActionResult Index() {
return View();
}
public IActionResult Error() {
ViewData["RequestId"] = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
return View();
}
}
}
C#using System.Collections.Generic;
using System.IO;
using System.Linq;
using DevExpress.XtraReports.UI;
using ServerSideApp.PredefinedReports;
using ServerSideApp.Data;
namespace ServerSideApp.Services
{
public class CustomReportStorageWebExtension : DevExpress.XtraReports.Web.Extensions.ReportStorageWebExtension
{
protected ReportDbContext DbContext { get; set; }
public CustomReportStorageWebExtension(ReportDbContext dbContext) {
this.DbContext = dbContext;
}
public override bool CanSetData(string url) {
// Determines whether a report with the specified URL can be saved.
// Add custom logic that returns **false** for reports that should be read-only.
// Return **true** if no valdation is required.
// This method is called only for valid URLs (if the **IsValidUrl** method returns **true**).
return true;
}
public override bool IsValidUrl(string url) {
// Determines whether the URL passed to the current report storage is valid.
// Implement your own logic to prohibit URLs that contain spaces or other specific characters.
// Return **true** if no validation is required.
return true;
}
public override byte[] GetData(string url) {
// Uses a specified URL to return report layout data stored within a report storage medium.
// This method is called if the **IsValidUrl** method returns **true**.
// You can use the **GetData** method to process report parameters sent from the client
// if the parameters are included in the report URL's query string.
var reportData = DbContext.Reports.FirstOrDefault(x => x.Name == url);
if(reportData != null)
return reportData.LayoutData;
if(ReportsFactory.Reports.ContainsKey(url)) {
using var ms = new MemoryStream();
using XtraReport report = ReportsFactory.Reports[url]();
report.SaveLayoutToXml(ms);
return ms.ToArray();
}
throw new DevExpress.XtraReports.Web.ClientControls.FaultException(string.Format("Could not find report '{0}'.", url));
}
public override Dictionary<string, string> GetUrls() {
// Returns a dictionary that contains the report names (URLs) and display names.
// The Report Designer uses this method to populate the Open Report and Save Report dialogs.
return DbContext.Reports
.ToList()
.Select(x => x.Name)
.Union(ReportsFactory.Reports.Select(x => x.Key))
.ToDictionary<string, string>(x => x);
}
public override void SetData(XtraReport report, string url) {
// Saves the specified report to the report storage with the specified name
// (saves existing reports only).
using var stream = new MemoryStream(); report.SaveLayoutToXml(stream);
var reportData = DbContext.Reports.FirstOrDefault(x => x.Name == url);
if(reportData == null) {
DbContext.Reports.Add(new ReportItem { Name = url, LayoutData = stream.ToArray() });
} else {
reportData.LayoutData = stream.ToArray();
}
DbContext.SaveChanges();
}
public override string SetNewData(XtraReport report, string defaultUrl) {
// Allows you to validate and correct the specified name (URL).
// This method also allows you to return the resulting name (URL),
// and to save your report to a storage. The method is called only for new reports.
SetData(report, defaultUrl);
return defaultUrl;
}
}
}
C#using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using DevExpress.AspNetCore;
using DevExpress.AspNetCore.Reporting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using DevExpress.XtraReports.Web.Extensions;
using DevExpress.Security.Resources;
using Microsoft.EntityFrameworkCore;
using ServerSideApp.Services;
using ServerSideApp.Data;
var builder = WebApplication.CreateBuilder(args);
AppDomain.CurrentDomain.SetData("DataDirectory", builder.Environment.ContentRootPath);
builder.Services.AddDevExpressControls();
builder.Services.AddScoped<ReportStorageWebExtension, CustomReportStorageWebExtension>();
builder.Services.AddMvc();
builder.Services.ConfigureReportingServices(configurator => {
if(builder.Environment.IsDevelopment())
configurator.UseDevelopmentMode();
configurator.ConfigureReportDesigner(designerConfigurator => {
designerConfigurator.RegisterDataSourceWizardConnectionStringsProvider<CustomSqlDataSourceWizardConnectionStringsProvider>();
designerConfigurator.RegisterDataSourceWizardJsonConnectionStorage<CustomDataSourceWizardJsonDataConnectionStorage>(true);
});
configurator.ConfigureWebDocumentViewer(viewerConfigurator => {
viewerConfigurator.UseCachedReportSourceBuilder();
viewerConfigurator.RegisterJsonDataConnectionProviderFactory<CustomJsonDataConnectionProviderFactory>();
viewerConfigurator.RegisterConnectionProviderFactory<CustomSqlDataConnectionProviderFactory>();
});
});
builder.Services.AddDbContext<ReportDbContext>(options => options.UseSqlite(builder.Configuration.GetConnectionString("ReportsDataConnectionString")));
builder.Services.AddCors(options => {
options.AddPolicy("AllowCorsPolicy", builder => {
// Allow all ports on local host.
builder.SetIsOriginAllowed(origin => new Uri(origin).Host == "localhost");
builder.AllowAnyHeader();
builder.AllowAnyMethod();
});
});
var app = builder.Build();
using(var scope = app.Services.CreateScope()) {
var services = scope.ServiceProvider;
services.GetService<ReportDbContext>().InitializeDatabase();
}
var contentDirectoryAllowRule = DirectoryAccessRule.Allow(new DirectoryInfo(Path.Combine(app.Environment.ContentRootPath, "Content")).FullName);
AccessSettings.ReportingSpecificResources.TrySetRules(contentDirectoryAllowRule, UrlAccessRule.Allow());
DevExpress.XtraReports.Configuration.Settings.Default.UserDesignerOptions.DataBindingMode = DevExpress.XtraReports.UI.DataBindingMode.Expressions;
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors("AllowCorsPolicy");
app.UseDevExpressControls();
System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.Run();