Using DocumentUltimate in an ASP.NET Core project
To use DocumentUltimate in an ASP.NET Core Project, do the following in Visual Studio:
Make sure you have added references to DocumentUltimate assemblies as described here.
Open Program.cs of your project and insert the marked lines:
using GleamTech; using GleamTech.AspNet; using GleamTech.AspNet.Core; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); //---------------------- //Add GleamTech to the ASP.NET Core services container. builder.Services.AddGleamTech(); //---------------------- var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } //---------------------- //Register GleamTech to the ASP.NET Core HTTP request pipeline. app.UseGleamTech(); //---------------------- app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run();Caution
services.AddGleamTechcallsservices.AddDistributedMemoryCacheandservices.AddSessioninternally and if you need to set a custom cache likeservices.AddStackExchangeRedisCacheor need to callservices.AddSessionwith custom options then place them beforeservices.AddGleamTechso that they become effective.Caution
The order of
app.UseXXXmethods is important soapp.UseGleamTechshould be placed beforeapp.UseStaticFiles,app.UseRoutingandapp.UseEndpoints(or beforeapp.UseMvcfor legacy ASP.NET Core 2.1).If you have a legacy ASP.NET Core project (before .NET 6 minimal hosting model), open Startup.cs of your project and insert the marked lines into
ConfigureServicesandConfiguremethods of yourStartupclass.public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); //---------------------- //Add GleamTech to the ASP.NET Core services container. services.AddGleamTech(); //---------------------- } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } //---------------------- //Register GleamTech to the ASP.NET Core HTTP request pipeline. app.UseGleamTech(); //---------------------- app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }Optionally set DocumentUltimate's global configuration (if overriding a default value is required). For example, you may want to set the license key and the document cache location.
You can specify the configuration in
appsettings.jsonfile:{ //Set this property only if you have a valid license key, otherwise do not //set it so DocumentUltimate runs in trial mode. "DocumentUltimate:LicenseKey": "QQJDJLJP34...", //The default CacheLocation value is "~/App_Data/DocumentCache" //Both virtual and physical paths are allowed (or a Location instance for one of the supported //file systems like Amazon S3 and Azure Blob). "DocumentUltimateWeb:CacheLocation": "~/App_Data/DocumentCache" }As you would notice,
DocumentUltimate:prefix maps toDocumentUltimateConfiguration.CurrentandDocumentUltimateWeb:prefix maps toDocumentUltimateWebConfiguration.Current.Alternatively you can specify the configuration in code, in
Configuremethod of your Startup.cs afterapp.UseGleamTechcall://---------------------- //Register GleamTech to the ASP.NET Core HTTP request pipeline. app.UseGleamTech(() => { //Set this property only if you have a valid license key, otherwise do not //set it so DocumentUltimate runs in trial mode. DocumentUltimateConfiguration.Current.LicenseKey = "QQJDJLJP34..."; //The default CacheLocation value is "~/App_Data/DocumentCache" //Both virtual and physical paths are allowed (or a Location instance for one of the supported //file systems like Amazon S3 and Azure Blob). DocumentUltimateWebConfiguration.Current.CacheLocation = "~/App_Data/DocumentCache"; }); //----------------------Create a new View (eg. Index.cshtml) and insert these lines:
@using GleamTech.AspNet.Core @using GleamTech.DocumentUltimate @using GleamTech.DocumentUltimate.AspNet @using GleamTech.DocumentUltimate.AspNet.UI <!DOCTYPE html> @{ var documentViewer = new DocumentViewer { Width = 800, Height = 600, Document = "~/Documents/Document.docx" }; } <html> <head> <title>Document Viewer</title> @this.RenderHead(documentViewer) </head> <body> @this.RenderBody(documentViewer) </body> </html>Tip
Alternatively you can add the namespaces globally in Views/_ViewImports.cshtml to avoid adding namespaces in your pages every time.
This will render a DocumentViewer component in the page, which loads and displays the source document
~/Documents/Document.docx. TheDocumentViewer.Documentproperty can point to a physical/virtual path, a URL or a Data URL, a file on Amazon S3 or Azure Blob Storage, a file in memory or stream or database, a custom file provider.Upon first view, internally DocumentViewer will convert the source document to PDF, a web-friendly format which DocumentViewer uses to actually render documents in the browser. So in this case, the user will see "please wait awhile..." message in the viewer for a few seconds. The generated PDF will be cached and upon consecutive page views, the document will be served directly from the cache so the user will see the document instantly on second viewing. When you modify the source document, the cached files are invalidated so your original document and the corresponding cached files are always synced automatically. Note that it's also possible to pre-cache documents via
DocumentCache.PreCacheDocumentmethod (e.g. when your user uploads a document), see Pre-caching documents for more information.