1. GroupDocs Documentation
  2. /
  3. GroupDocs.Viewer Product Family
  4. /
  5. GroupDocs.Viewer for .NET
  6. /
  7. Showcases
  8. /
  9. How to integrate GroupDocs.Viewer in ASP.NET Core Web application

How to integrate GroupDocs.Viewer in ASP.NET Core Web application

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Viewer MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-viewer -- dnx GroupDocs.Viewer.Mcp --yes
Folders, licensing and more →

This guide builds a small ASP.NET Core web application that shows documents in the browser. The viewer itself is GroupDocs.Viewer.UI, and GroupDocs.Viewer for .NET renders the documents for it. The steps use the .NET CLI; in Visual Studio they are the same.

The browser loads the viewer app from /viewer, then asks /viewer-api for the pages. The API renders them with GroupDocs.Viewer for .NET, reads documents from ./Files and keeps rendered pages in ./Cache

The application has two endpoints:

  1. /viewer serves the viewer, an Angular app that runs in the browser.
  2. /viewer-api renders the documents for it. It reads the documents from the Files folder and stores the rendered pages in the Cache folder, so each page is rendered only once.

The finished application is also available as a sample in the GroupDocs.Viewer.UI repository.

Prerequisites

Install the .NET SDK, version 8 or later. The packages also support .NET 6.

Create the application

Create an empty web application in the my-viewer-app folder:

dotnet new web -o my-viewer-app
cd my-viewer-app

Install the packages

Add the viewer packages from NuGet:

dotnet add package GroupDocs.Viewer.UI
dotnet add package GroupDocs.Viewer.UI.SelfHost.Api
dotnet add package GroupDocs.Viewer.UI.Api.Local.Storage
dotnet add package GroupDocs.Viewer.UI.Api.Local.Cache
PackageWhat it adds
GroupDocs.Viewer.UIThe viewer app and the middleware that serves it at /viewer
GroupDocs.Viewer.UI.SelfHost.ApiThe API at /viewer-api, which renders documents with GroupDocs.Viewer for .NET. It runs on Windows, Linux and macOS; the former GroupDocs.Viewer.UI.SelfHost.Api.CrossPlatform package is deprecated
GroupDocs.Viewer.UI.Api.Local.StorageA file storage on the local disk: the API reads documents from it and saves the files users upload
GroupDocs.Viewer.UI.Api.Local.CacheA cache on the local disk for the rendered pages, thumbnails and PDF files

Configure the application

Replace the contents of Program.cs with the following code:

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddGroupDocsViewerUI();

builder.Services
    .AddControllers()
    .AddGroupDocsViewerSelfHostApi()
    .AddLocalStorage("./Files")
    .AddLocalCache("./Cache");

var app = builder.Build();

app.UseRouting();

app.MapGroupDocsViewerUI(options =>
{
    options.UIPath = "/viewer";
    options.ApiEndpoint = "/viewer-api";
});

app.MapGroupDocsViewerApi(options =>
{
    options.ApiPath = "/viewer-api";
});

app.Run();

This code does the following:

  • AddGroupDocsViewerUI registers the viewer app, and MapGroupDocsViewerUI serves it at /viewer. ApiEndpoint tells the app where the API is.
  • AddGroupDocsViewerSelfHostApi registers the API with its rendering engine, and MapGroupDocsViewerApi maps it at /viewer-api.
  • AddLocalStorage("./Files") and AddLocalCache("./Cache") set the folders for the documents and the rendered pages.

Set the license

Without a license, GroupDocs.Viewer works in evaluation mode, with the evaluation limitations. To apply a license, set the GROUPDOCS_LIC_PATH environment variable to the path of the license file, or set the path in code:

builder.Services
    .AddControllers()
    .AddGroupDocsViewerSelfHostApi(config =>
    {
        config.SetLicensePath("GroupDocs.Viewer.lic");
    })
    .AddLocalStorage("./Files")
    .AddLocalCache("./Cache");

Restrict access

The viewer does not authenticate users. To let only signed-in users open documents, apply your authorization policy to both endpoints. Applying it to /viewer alone leaves the API, and with it every document, open to anyone:

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapGroupDocsViewerUI(options =>
{
    options.UIPath = "/viewer";
    options.ApiEndpoint = "/viewer-api";
}).RequireAuthorization();

app.MapGroupDocsViewerApi(options =>
{
    options.ApiPath = "/viewer-api";
}).RequireAuthorization();

Create the folders

Create the folders for the documents and the cache, then copy a few documents into Files:

mkdir Files
mkdir Cache

Run the application

Run the application with the dotnet run command in the my-viewer-app folder. The output looks like this:

Building...
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5081
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\my-viewer-app

In the browser, open the address from the output with the /viewer path, for example http://localhost:5081/viewer:

GroupDocs.Viewer.UI in the ASP.NET Core application: an annual report with page thumbnails and the zoom, rotate, page, search, print, present, Download PDF and Open File buttons

  • Click Open File to choose a document from the Files folder or to upload one.
  • To open a document directly, add its name to the address: http://localhost:5081/viewer?file=annual-report.docx.
  • The first time you open a document, its pages are rendered, so it takes longer. After that, the viewer reads them from the Cache folder.

Customize the viewer

Options of AddGroupDocsViewerUI set how the viewer renders documents (HTML or images), which file it opens first, its zoom, its theme, and the buttons it shows. For example, the following code hides the rotate buttons and the button that switches between the light and dark themes:

builder.Services
    .AddGroupDocsViewerUI(config =>
    {
        config.EnableRotate = false;
        config.EnableThemeSwitcher = false;
    });

For all the options, see Customize UI behavior and Show or hide UI controls in the GroupDocs.Viewer.UI repository.

Resources