• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

AndreuCodina / CrossValidation / 5940258519

22 Aug 2023 02:32PM UTC coverage: 92.715% (-2.7%) from 95.391%
5940258519

Pull #74

github

web-flow
Merge e4885161e into 230517030
Pull Request #74: Problem Details MVP

431 of 486 branches covered (88.68%)

Branch coverage included in aggregate %.

299 of 299 new or added lines in 21 files covered. (100.0%)

1389 of 1477 relevant lines covered (94.04%)

176.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.48
/src/CrossValidation/DependencyInjection/CrossValidationBuilderExtensions.cs
1
using System.Collections;
2
using System.Globalization;
3
using System.Net.Mime;
4
using System.Text;
5
using Microsoft.AspNetCore.Builder;
6
using Microsoft.AspNetCore.Http;
7
using Microsoft.AspNetCore.Http.HttpResults;
8
using Microsoft.AspNetCore.Localization;
9

10
namespace CrossValidation.DependencyInjection;
11

12
public static class CrossValidationBuilderExtensions
13
{
14
    public static IApplicationBuilder UseCrossValidation(this WebApplication app)
15
    {
16
        app.UseCustomRequestLocalization();
69✔
17
        // app.UseGlobalExceptionMiddleware();
18
        app.UseExceptionHandler();
69✔
19
        app.UseStatusCodePages();
69✔
20
        AddErrorCodePage(app);
69✔
21
        return app;
69✔
22
    }
23

24
    private static void AddErrorCodePage(WebApplication app)
25
    {
26
        if (!CrossValidationOptions.IsErrorCodePageEnabled)
69✔
27
        {
28
            return;
51✔
29
        }
30
        
31
        app.MapGet(CrossValidationOptions.ErrorCodePagePath, CreateErrorCodePage);
18✔
32
    }
18✔
33

34
    private static ContentHttpResult CreateErrorCodePage()
35
    {
36
        var resources = new Dictionary<string, string>();
×
37

38
        foreach (var resourceManager in CrossValidationOptions.ResourceManagers)
×
39
        {
40
            var resourceSet = resourceManager.GetResourceSet(
×
41
                culture: new CultureInfo(CrossValidationOptions.DefaultCultureCode),
×
42
                createIfNotExists: true,
×
43
                tryParents: true)!;
×
44

45
            foreach (DictionaryEntry resource in resourceSet)
×
46
            {
47
                resources.TryAdd(resource.Key.ToString()!, resource.Value!.ToString()!);
×
48
            }
49
        }
50

51
        var html = new StringBuilder(
×
52
            """
×
53
            <!DOCTYPE html>
×
54
            <html>
×
55
            <head>
×
56
                <title>Error codes</title>
×
57
            </head>
×
58
            <body>
×
59
            """);
×
60

61
        foreach (var resource in resources)
×
62
        {
63
            html.AppendLine(
×
64
                $$"""
×
65
                  <b><a href="#{{resource.Key}}">{{resource.Key}}</a></b>
×
66
                  <br>
×
67
                  <p><b>Template:</b> {{resource.Value}}</p>
×
68
                  <hr>
×
69
                  """);
×
70
        }
71

72
        html.AppendLine(
×
73
            """
×
74
            </body>
×
75
            </html>
×
76
            """);
×
77
        
78
        return TypedResults.Content(html.ToString(), MediaTypeNames.Text.Html);
×
79
    }
80

81
    private static IApplicationBuilder UseGlobalExceptionMiddleware(this IApplicationBuilder app)
82
    {
83
        app.UseMiddleware<GlobalExceptionMiddleware>();
×
84
        return app;
×
85
    }
86
    
87
    private static IApplicationBuilder UseCustomRequestLocalization(this IApplicationBuilder app)
88
    {
89
        var supportedCultures = CrossValidationOptions.SupportedCultureCodes
69✔
90
            .Select(x => new CultureInfo(x))
84✔
91
            .ToList();
69✔
92
        var localizationOptions = new RequestLocalizationOptions
69✔
93
        {
69✔
94
            DefaultRequestCulture = new RequestCulture(CrossValidationOptions.DefaultCultureCode),
69✔
95
            SupportedCultures = supportedCultures,
69✔
96
            SupportedUICultures = supportedCultures,
69✔
97
            FallBackToParentCultures = true,
69✔
98
            FallBackToParentUICultures = true,
69✔
99
            RequestCultureProviders = new List<IRequestCultureProvider>
69✔
100
            {
69✔
101
                new AcceptLanguageHeaderRequestCultureProvider()
69✔
102
            }
69✔
103
        };
69✔
104
        app.UseRequestLocalization(localizationOptions);
69✔
105
        return app;
69✔
106
    }
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc