Simplify UseRequestLocalization Configuration

Feb 12, 2017     Viewed 1556 times    1 Comments
Posted in #Localization  # Fluent APIs 

The Localization repository is one of my favorite ASP.NET Core repositories, but the thing that I dislike is the way to configure it using UseRequestLocalization which is used the options setter delegate pattern, I don't have a doubt about the pattern itself, but I hate the much of the code that I need tontinue, StopFilters }

Also may we need an options for the request filtering to access all the registered filters.

public class RequestFilteringOptions
{
    public IList Filters { get; } = new List();
}

After that need more three classes one is an extensions for the RequestFilteringOptions and two required for the middleware.

public static class RequestFilteringOptionsExtensions
{
    public static RequestFilteringOptions AddRequestFilter(this RequestFilteringOptions requestFilteringOptions, IRequestFilter filter)
    {
        if (filter == null)
        {
            throw new ArgumentNullException(nameof(filter));
        }

        requestFilteringOptions.Filters.Add(filter);
        return requestFilteringOptions;
    }
}
public class RequestFilteringMiddleware
{
    private readonly RequestDelegate _next;
    private readonly RequestFilteringOptions _options;

    public RequestFilteringMiddleware(
        RequestDelegate next,
        RequestFilteringOptions options)
    {
        if (next == null)
        {
            throw new ArgumentNullException(nameof(next));
        }

        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }

        _next = next;
        _options = options;
    }

    public Task Invoke(HttpContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        var requestFilteringContext = new RequestFilteringContext
        {
            HttpContext = context,
            Result = RequestFilteringResult.Continue
        };

        foreach (var filter in _options.Filters)
        {
            filter.ApplyFilter(requestFilteringContext);

            switch (requestFilteringContext.Result)
            {
                case RequestFilteringResult.Continue:
                    break;
                case RequestFilteringResult.StopFilters:
                    return Task.FromResult(0);
                default:
                    throw new ArgumentOutOfRangeException($"Invalid filter termination {requestFilteringContext.Result}");
            }
         }

         return _next(context);
    }
}
public static class RequestFilteringMiddlewareExtensions
{
    public static IApplicationBuilder UseRequestFiltering(this IApplicationBuilder app, RequestFilteringOptions options)
    {
        if (app == null)
        {
            throw new ArgumentNullException(nameof(app));
        }

        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }

        return app.UseMiddleware(options);
    }
}

The middleware classes is straightforward for those who writing ASP.NET middleware before, the idea for our middleware is simple, iterate over all the request filtering that have been registered an calling the ApplyFilter which execute the actual filter, after that we gathering the result from each filter and check the Result property in the context to know if we need to go further to the next filter, or the actual filter is applied and no need to keep filtering.

Implementation APIs

Now we are ready to implement the basic request filtering techniques that are available in the IIS. FYI I will dig only into the essentials classes for each type.

Filter based on File Extensions

As we saw in the previous post that file extensions filter have AllowUnlisted, FileExtension and Allowed properties, so we can use our abstraction to define a set of options as the following:

public class FileExtensionsOptions : IRequestFilterOptions
{
     public bool AllowUnlisted { get; set; } = true;

     public IList FileExte
    

Twitter Facebook Google + LinkedIn


1 Comment

Suraj (5/4/2017 10:58:55 PM)

nice article, well written. Thanks.


Leave a Comment