Curiosus.Tools.Web
ASP.NET Core helpers for MVC sites and APIs: middleware (exception handling, request logging, performance measuring, reverse proxy), model binders, data protection setup, reCAPTCHA, sitemap and assorted HTTP extensions.
Installation
dotnet add package Curiosus.Tools.Web
Usage
using Curiosus.Tools.Web.Middleware;
using Curiosus.Tools.Web.MimeTypes;
using Curiosus.Tools.Web.ModelBinders;
using Curiosus.Tools.Web.ReverseProxy;
// services
services.AddReverseProxy(configuration.ReverseProxy); // ForwardedHeaders for ProxyIps; no-op when null
services.AddMimeTypeMapping(); // MimeMappingService
services.AddMvc(options => options.ModelBinderProviders.Insert(0, new TrimStringModelBinderProvider()));
// pipeline
app.UseReverseProxy(configuration.ReverseProxy); // forwarded headers + PathBase
app.UseCuriosusExceptionHandler("/error"); // logs and re-executes the pipeline with /error
app.UsePerformanceMeasurer();
app.UseRequestLogging(); // dumps request bodies, masks SensitiveDataProtector fields
app.UseRouting();
UseRequestLogging resolves a non-generic ILogger from DI (registered by
Curiosus.Hosting.Web).
What else is inside:
- Model binding —
TrimStringModelBinderProvider,InvariantDecimalModelBinderProvider,DateTimeModelBinderProvider,DelimitedArrayModelBinder,TrimStringSystemJsonConverterfor[FromBody]strings. - Middleware —
UseUserTracer(trace id cookie, needsUniqueIdGenerator.Initialize),UseLocalizationSetter(language cookie),UsePreviewDetector(rewrites the path for link-preview bots). - Options-driven services —
AddCuriosusDataProtection(DataProtectionOptions)/DisableDataProtection(),AddReCaptcha(ReCaptchaOptions)+ReCaptchaService,AddSiteMap(mvcBuilder)+SiteMapBuilder,ConfigureCookiesForOldBrowsers()(SameSite fixes). - MVC —
MVCBaseController(error views, AJAX responses, redirects),SiteError,ModelStateExtensions.AddModelErrorIf,Page<T>/Paginator,InvariantDecimalTagHelper. - Extensions —
HttpContext/HttpRequest(IsAjaxRequest,GetAbsoluteUrl, ...),ISessionJSON objects,HtmlSanitizerHelper.
TrimStringNewtonsoftConverter is obsolete and will be removed with the Newtonsoft.Json dependency.
HTTP requests go through the HttpClient named ReCaptchaService.HttpClientName from IHttpClientFactory,
which AddReCaptcha registers. Configure it for timeouts, a proxy or resilience handlers:
services.AddHttpClient(ReCaptchaService.HttpClientName, client => client.Timeout = TimeSpan.FromSeconds(30));
See also
- Curiosus.Hosting.Web — ASP.NET Core app bootstrapper
- Curiosus.Tools — non-web helpers
- Curiosus.Utils — all packages