DNTCommon.Web.Core
DNTCommon.Web.Core provides common scenarios\’ solutions for ASP.NET Core applications.
Install via NuGet
To install DNTCommon.Web.Core, run the following command in the Package Manager Console:
PM> Install-Package DNTCommon.Web.Core
You can also view the package page on NuGet.
Linux (and containers) support
The SkiaSharp library needs extra dependencies to work on Linux and containers. Please install the following NuGet
packages:
PM> Install-Package SkiaSharp.NativeAssets.Linux.NoDependencies
PM> Install-Package HarfBuzzSharp.NativeAssets.Linux
You also need to modify your .csproj file to include some MSBuild directives that ensure the required files are in a
good place. These extra steps are normally not required but seems to be some issues on how .NET loads them.
<Target Name=\"CopyFilesAfterPublish\" AfterTargets=\"AfterPublish\"> <Copy SourceFiles=\"$(TargetDir)runtimes/linux-x64/native/libSkiaSharp.so\" DestinationFolder=\"$([System.IO.Path]::GetFullPath(\'$(PublishDir)\'))/bin/\" /> <Copy SourceFiles=\"$(TargetDir)runtimes/linux-x64/native/libHarfBuzzSharp.so\" DestinationFolder=\"$([System.IO.Path]::GetFullPath(\'$(PublishDir)\'))/bin/\" /> </Target>
Usage
After installing the DNTCommon.Web.Core package, to register its default providers, call services.AddDNTCommonWeb();
method in
your Startup class.
using DNTCommon.Web.Core; namespace MyWebApp { public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddDNTCommonWeb(); }
Features
-
ActionResults
- FeedResult
is an ASP.NET Core RSS feed renderer. - OpenSearchResult
is an ASP.NET Core OpenSearch description format provider. - SitemapResult
is an ASP.NET Core Sitemap renderer.
- FeedResult
-
Caching
- ICacheService
encapsulates IMemoryCache functionalities. - [NoBrowserCache]
action filter setsno-cache,must-revalidate,no-storeheaders for the currentResponse.
- ICacheService
-
DependencyInjection
- IServiceProviderExtensions
creates an IServiceScope which contains an IServiceProvider used to resolve dependencies from a newly created
scope and then runs an associated callback.
- IServiceProviderExtensions
-
Drawing
- TextToImageExtensions
and TextToImageResult
draw a text on a bitmap and then return it as a png byte array.
- TextToImageExtensions
-
Http
- ICommonHttpClientFactory
service, reuses a single HttpClient instance across a multi-threaded application. - DomainHelperExtensions
provides useful extension methods to extract domain info of the URL\’s. - IDownloaderService
encapsulates HttpClient\’s best practices of downloading large files. - IHtmlHelperService
provides helper methods to work with HTML contents such as extracting links and titles or changing relative Urls
to absolute Urls. - IHttpRequestInfoService
provides useful methods to work with current HttpContext.Request. - IRedirectUrlFinderService
finds the actual hidden URL after multiple redirects. - IUrlNormalizationService
transforms a URL into a normalized URL so it is possible to determine if two syntactically different URLs may be
equivalent. - IHtmlReaderService
reads the HTML document\’s nodes recursively. -
IUAParserServiceis the updated version of the UAParser library with the latest regexes.yaml file.
- ICommonHttpClientFactory
-
Mail
- IWebMailService
simplifies sending an email using theMailKitlibrary. It\’s able to use razor based email templates.
- IWebMailService
-
ModelBinders
- PersianDateModelBinderProvider
parses an incoming Persian date and then binds it to a DateTime property automatically. To use it globally (
assuming your app only sends Persian dates to the server), Add it to
MvcOptions:services.AddMvc(options => options.UsePersianDateModelBinder())or just apply it to an specific
view-model[ModelBinder(BinderType = typeof(PersianDateModelBinder))]. - YeKeModelBinderProvider
parses an incoming text and then corrects its Ye & Ke characters automatically. To use it globally, Add it to
MvcOptions:services.AddMvc(options => options.UseYeKeModelBinder())or just apply it to an specific
view-model[ModelBinder(BinderType = typeof(YeKeModelBinder))]. - ApplyCorrectYeKeFilterAttribute
parses an incoming text and then corrects its Ye & Ke characters automatically. To use it globally, Add it to
MvcOptions:services.AddControllersWithViews(options => options.Filters.Add(typeof(ApplyCorrectYeKeFilterAttribute))).
- PersianDateModelBinderProvider
-
Mvc
- ControllerExtensions
provides useful extension methods to work with MVC Controllers. - IMvcActionsDiscoveryService
provides a way to list all of the controllers and action methods of an MVC application. - IViewRendererService
helps rendering a .cshtml file as an string. It\’s useful for creating razor based email templates. - UploadFileService
saves the posted IFormFile to the specified directory asynchronously.
- ControllerExtensions
-
Blazor
- IBlazorStaticRendererService
helps rendering a razor component as an string. It\’s useful for creating razor based email templates. - IBlazorRenderingContext
helps detecting the current rendering mode of a razor component.
- IBlazorStaticRendererService
-
Schedulers
- BackgroundQueueController
A .NET Core replacement for the oldHostingEnvironment.QueueBackgroundWorkItemmethod. - ScheduledTasksController
DNTScheduler.Core is a lightweight ASP.NET Core\’s background tasks runner and scheduler. To define a new
task, create a new class
that implements theIScheduledTaskinterface. To register this new task, callservices.AddDNTScheduler();
method in
your Startup class.AddDNTScheduler
method, adds this new task to the list of the defined tasks. Also its first parameter defines the custom logic of
the running intervals of this task. It\’s a callback method that will be called every second and provides the
utcNow value. If it returns true, the job will be executed.If you have multiple jobs at the same time, theorder
parameter\’s value indicates the order of their execution.
- BackgroundQueueController
-
Security
- [AjaxOnly]
action filter determines whether the HttpRequest\’sX-Requested-Withheader hasXMLHttpRequestvalue. - IProtectionProviderService
is an encryption provider based onMicrosoft.AspNetCore.DataProtection.IDataProtector. It\’s only useful for
short-term encryption scenarios such as creating encrypted HTTP cookies. - IFileNameSanitizerService
determines whether the requested file is safe to download to avoidDirectory Traversal&File Inclusion
attacks. - UploadFileExtensions
attribute determines only selected file extensions are
safe to be uploaded. - AllowUploadSafeFiles
attribute disallows uploading dangerous files such as .aspx, web.config and .asp files. - AntiDosMiddleware
is a rate limiter and throttling middleware for ASP.NET Core apps. To use it first addapp.UseAntiDos()
andservices.Configure<AntiDosConfig>
to Startup.cs
file. Then complete theAntiDosConfigsection of
the appsettings.json
file. - IAntiXssService
provides a cleaning service for unsafe HTML fragments that can lead to XSS attacks based on a whitelist of allowed
tags and attributes. To use it addservices.Configure<AntiXssConfig>
to Startup.cs
file. Then complete theAntiXssConfigsection of
the appsettings.json
file. - IPasswordHasherService provides a custom Pbkdf2
hashing and validating service.
- [AjaxOnly]
