dotnet / dotnet/aspnetcore

[Analyzer] : Warn when registering custom IProblemDetailsWriter after calling AddRazorPages, etc

Open
#48,180 11 comments 2 reactions 0 assignees View on GitHub
analyzer api-approved area-mvc bug help wanted
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

## Background and Motivation

Registering a custom `IProblemDetailsWriter` after calling `AddRazorPages`, etc results in the `DefaultProblemDetailsWriter`, not the custom `IProblemDetailsWriter` being called.

## Proposed Analyzer

### Analyzer Behavior and Message

Issue a warning when Registering a custom `IProblemDetailsWriter` after calling `AddRazorPages`, etc.

[ASP0001: Authorization middleware is incorrectly configured](https://learn.microsoft.com/en-us/aspnet/core/diagnostics/asp0001?view=aspnetcore-7.0) is similar.

See https://github.com/dotnet/AspNetCore.Docs/issues/29152

Note: When using a custom `IProblemDetailsWriter`, the custom `IProblemDetailsWriter `must be registered before calling [AddRazorPages](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.mvcservicecollectionextensions.addrazorpages?branch=main), [AddControllers](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.mvcservicecollectionextensions.addcontrollers?branch=main), or [AddControllersWithViews](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.mvcservicecollectionextensions.addcontrollerswithviews?branch=main).

### Category

- [ ] Design
- [ ] Documentation
- [ ] Globalization
- [ ] Interoperability
- [ ] Maintainability
- [ ] Naming
- [ ] Performance
- [ ] Reliability
- [ ] Security
- [ ] Style
- [x] Usage

### Severity Level

- [ ] Error
- [x] Warning
- [ ] Info
- [ ] Hidden

## Usage Scenarios

Issue a warning when Registering a custom `IProblemDetailsWriter` after calling `AddRazorPages`, etc.

## Risks

None.

Contributor guide

Open the contributing guide

Research direction

Start by tracing the analyzer entry points for registrations of IProblemDetailsWriter and calls to AddRazorPages, AddControllers, and AddControllersWithViews. The work is done when the analyzer warns about a custom writer registered afterward, with behavior and severity matching the proposed warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.