受欢迎的博客标签

NopCommerce 4.x (6)-ASP.NET Core Dependency injection in NopCommerce

Published

 

Dependency injection in IEndpointRouteBuilder

NopCommerce 4.3

using System.Linq;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Nop.Core.Domain.Localization;
using Nop.Data;
using Nop.Services.Localization;

namespace Nop.Web.Infrastructure
{
    public class BaseRouteProvider
    {
        protected string GetRouterPattern(IEndpointRouteBuilder endpointRouteBuilder, string seoCode = "")
        {
            if (DataSettingsManager.DatabaseIsInstalled)
            {
                var localizationSettings = endpointRouteBuilder.ServiceProvider.GetRequiredService<LocalizationSettings>();
                if (localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
                {
                    var langservice = endpointRouteBuilder.ServiceProvider.GetRequiredService<ILanguageService>();
                    var languages = langservice.GetAllLanguages().ToList();
                    return "{language:lang=" + languages.FirstOrDefault().UniqueSeoCode + $"}}/{seoCode}";
                }
            }
            return seoCode;
        }
    }
}

 

 

IApplicationBuilder

public static void UsePageNotFound(this IApplicationBuilder application)
        {
            var serviceProvider = application.ApplicationServices;
            var webHelper = serviceProvider.GetRequiredService<IWebHelper>();
                    if (!webHelper.IsStaticResource())