Giter Club home page Giter Club logo

fstonge.aspnetcore's Introduction

ASP.NET Core Utilities

Route translations

How to use it:

In Startup.cs

Under ConfigureServices, you need to add the following:

public void ConfigureServices(IServiceCollection services)
{
    var builder = services.AddControllersWithViews(options =>
    {
        // Add filter to set current filter in cookies
        options.AddCultureCookieFilter();
    });
    
    services.AddLocalization(options => options.ResourcesPath = "Resources");
    builder.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
    builder.AddDataAnnotationsLocalization();

    // Inject required services, add routing and replace current LinkGenerator
    services.AddRoutingLocalization(_configuration);
}

Under Configure, you need to add the following:

public void Configure(IApplicationBuilder app)
{
    // Setup Request localization, Rewriter and Routing
    app.UseRoutingLocalization();
    app.UseStaticFiles();
    
    // Setup Endpoints with culture
    app.UseEndpointsLocalization();
}

In appsettings.json

You need to add the following structure in your appsettings:

{
  "RoutingTranslation":
  {
    "SupportedCultures": "fr, en",
    "DefaultCulture": "fr"
  }
}

Attribute in Controllers

[Translate("en", "orders")]
[Translate("fr", "commandes")]
public class OrdersController : Controller
{
    [Translate("fr", "liste")]
    public IActionResult List()
    {
        return View();
    }
}

Generate your first urls

<form asp-controller="orders" asp-action="list"></form>

<a asp-controller="products" asp-action="detail" asp-route-id="12"></a>

<a asp-route-culture="fr">French</a>

Options

You can inject an IOptions that contains your configured cultures. Use the following:

IOptions<RequestLocalizationOptions> options

Custom route translation

You can create your own custom validation by deriving from ICustomTranslation:

public class ProductTranslation : ICustomTranslation
{    
    public string ControllerName => "products";
    
    public string ActionName => "detail";
    
    public RewriteRule[] RewriteRules => new[]
    {
        new RewriteRule(
            @"^([-a-zA-Z]+)\/([^\/]+)\/[-\/a-zA-Z0-9]+\/p-([=._a-zA-Z0-9]+)-.*$",
            "$1/$2/detail/$3")
    };
    
    public string GenerateUrlPath(RouteValueDictionary values, FragmentString fragment)
    {
        return "/" +
           $"{values.GetParameterValue(RouteValue.Culture)}/" +
           $"{values.GetParameterValue(RouteValue.Controller)}/" + 
           "10-control-and-testing/" +
           "14-testing-string/" +
           $"p-{values.GetParameterValue(RouteValue.Id)}-testing-product-string";
    }
}

Then add it as a scoped in Startup.cs:

// Add custom translations as singleton
public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<ICustomTranslation, ProductTranslation>();
}

Async Distribued Session

How to use it:

In Startup.cs

Under ConfigureServices, you need to add the following:

public void ConfigureServices(IServiceCollection services)
{
    var builder = services.AddControllersWithViews();
    
    // Store temp data in session
    builder.AddSessionStateTempDataProvider();

    // Add any distributed cache (this is in memory, but I can be Redis or others)
    services.AddDistributedMemoryCache();

    // Use the package to use the async distributed session
    services.AddAsyncDistributedSession();
}

Under Configure, you need to add the following:

public void Configure(IApplicationBuilder app)
{
    
    // Setup async distributed session
    app.UseAsyncDistributedSession();

    app.UseEndpoints();
}

fstonge.aspnetcore's People

Contributors

frederikstonge avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.