dotnet / dotnet/aspnetcore

Prefix support in MapRazorComponents / AddAdditionalAssemblies

Open
#57,167 5 comments 1 reaction 0 assignees View on GitHub
api-suggestion area-blazor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

## Background and Motivation

Previous versions of Razor (`services.AddRazorPages()`) had a `.Conventions` property in the options where you could write conventions to prefix razor pages with, well, a prefix.
This can be very useful in a plugin scenario where you have multiple assemblies (Razor Class Libraries) with their own razor pages, that may have the same paths, and map them with prefixes to prevent path conflicts.
Having a way to prefix the razor pages without manually ensuring there are no conflicting paths would lead to decreased maintenance and development time.

## Proposed API

There are multiple places where this could be added. As mentioned, it could previously be done with conventions in `.AddRazorPages(o => o.Conventions...)`, but as it is directly related to the URL mapping it probably belongs together with `.MapRazorComponents()` in `.UseEndpoints()`.

```
namespace Microsoft.AspNetCore.Builder;

public static class RazorComponentsEndpointConventionBuilderExtensions
public static RazorComponentsEndpointConventionBuilder AddAdditionalAssembly(
this RazorComponentsEndpointConventionBuilder builder,
string urlPrefix,
Assembly assembly) { }
}
```

## Usage Examples

```
app.UseEndpoints(endpointBuilder =>
{
var builder = endpointBuilder.MapRazorComponents();
foreach(IPlugin plugin in plugins)
{
builder.AddAdditionalAssembly(
plugin.GetType().Assembly.GetName().Name,
plugin.GetType().Assembly)
}
builder.AddInteractiveServerRenderMode();
});
```

## Alternative Designs

An alternative could be adding the old `.Conventions` to `services.AddRazorComponents()`.

## Risks

Can't think of any risks of the top of my head with the `.AddAdditionalAssembly()` chaining.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.