Skip to content

GroupDocs.Editor RESTful for .Net

Viktor Stupak edited this page Apr 11, 2024 · 3 revisions

Creating GroupDocs.Editor RestFul Service with ASP.NET Core Web API

GroupDocs.Editor is a powerful document editing and manipulation API that developers can integrate into their applications. This guide will walk you through the process of setting up a GroupDocs.Editor RestFul Service using ASP.NET Core Web API.

Here's the step-by-step guide to creating a GroupDocs.Editor RestFul Service with ASP.NET Core Web API.

1. Setting up the ASP.NET Core Web API

Begin by creating a new ASP.NET Core Web API project with .NET 6 framework. Open your terminal or command prompt and execute the following commands:

dotnet new webapi -n GroupDocsEditorService
cd GroupDocsEditorService

2. Installing GroupDocs.Editor.UI.Api package

To integrate GroupDocs.Editor into your project, install the GroupDocs.Editor.UI.Api package. Run the following command in your terminal:

dotnet add package GroupDocs.Editor.UI.Api

3. Adding Controllers and Swagger

Add controllers for handling document editing requests and Swagger for API documentation. Update your startup code as follows:

builder.Services.AddEditorControllers();
builder.Services.AddEditorSwagger();
builder.Services.AddEditor<AwsS3Storage>(builder.Configuration);

4. Feature Management

Utilize FeatureManagement to enable or disable parts of the Family formats supported by GroupDocs.Editor. Configure it as follows in your appsettings.json file:

"FeatureManagement": {
    "WordProcessing": true,
    "LocalFile": true,
    "Pdf": true,
    "Presentation": true,
    "Spreadsheet": true,
    "Email": true
}

5. Configure the HTTP request pipeline

Configure the HTTP request pipeline to include Swagger UI in the development environment and map controllers. Update your startup code as follows:

if (app.Environment.IsDevelopment())
{
    app.UseEditorSwaggerUI();
}

app.MapControllers();

Conclusion

Here's the code snippet for Program.cs file incorporating the optional CORS policy for local development:

using GroupDocs.Editor.UI.Api.Extensions;
using GroupDocs.Editor.UI.Api.Services.Implementation;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Add license for GroupDocs.Editor or remove for use Trial mode.
builder.Services.AddEditorLicense<Base64FileLicenseService>(builder.Configuration);

// Add services to the container.
builder.Services.AddEditorControllers();
builder.Services.AddEditorSwagger();
builder.Services.AddEditor<AwsS3Storage>(builder.Configuration);

// Optional: Add CORS policy for local development.
builder.Services.AddCors(p => p.AddPolicy("corsApp", policy =>
{
    policy.WithOrigins("*").AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
}));

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseHttpsRedirection();

if (app.Environment.IsDevelopment())
{
    app.UseEditorSwaggerUI();
}

// Optional: Use CORS policy for local development.
app.UseCors(x => x
    .AllowAnyOrigin()
    .AllowAnyMethod()
    .AllowAnyHeader());

app.MapControllers();

app.Run();

This version includes the optional CORS policy setup for local development, allowing requests from any origin during development.

In this guide, we've covered the process of creating a GroupDocs.Editor RestFul Service with ASP.NET Core Web API. By following these steps, you can integrate GroupDocs.Editor into your application, providing powerful document editing capabilities. Utilizing FeatureManagement allows you to customize the supported formats based on your application's requirements. Additionally, Swagger provides convenient API documentation for easier integration and testing.

Feel free to explore further functionalities and customization options provided by GroupDocs.Editor to optimize your document editing service.

Clone this wiki locally