3

我想从 RouteCollection 中删除现有路由,并希望通过插件在 nopCommerce 4.00 中添加具有相同路由名称的新路由

现有路线名称:

//home page
            routeBuilder.MapLocalizedRoute("HomePage", "",
        new { controller = "Home", action = "Index" });

我想用

   routeBuilder.MapLocalizedRoute("HomePage", "",
    new { controller = "CustomPage", action = "Homepage" });

我尝试了几种方法,但没有得到任何运气。

4

3 回答 3

5

就我而言,我必须更换robots.txt一代。我在插件中创建了一个新的公共控制器,并在此处复制原始操作:

public class MiscCommonController : BasePublicController
{
    #region Fields
    private readonly ICommonModelFactory _commonModelFactory;
    #endregion Fields

    #region Ctor
    public MiscCommonController(
        ICommonModelFactory commonModelFactory
        )
    {
        this._commonModelFactory = commonModelFactory;
    }
    #endregion Ctor

    #region Methods
    //robots.txt file
    //available even when a store is closed
    [CheckAccessClosedStore(true)]
    //available even when navigation is not allowed
    [CheckAccessPublicStore(true)]
    public virtual IActionResult RobotsTextFile()
    {
        var robotsFileContent = _commonModelFactory.PrepareRobotsTextFile();
        return Content(robotsFileContent, MimeTypes.TextPlain);
    }
    #endregion Methods
}

在此之后,我为我的插件创建了一个RouteProvider,并将原始路由替换为我自己的。

public partial class RouteProvider : IRouteProvider
{
    /// <summary>
    /// Gets a priority of route provider
    /// </summary>
    public int Priority => -1;

    /// <summary>
    /// Register routes
    /// </summary>
    /// <param name="routeBuilder">Route builder</param>
    public void RegisterRoutes(IRouteBuilder routeBuilder)
    {
        Route route = null;

        foreach (Route item in routeBuilder.Routes)
        {
            if (item.Name == "robots.txt")
            {
                route = item;
                break;
            }
        }

        if (route != null) routeBuilder.Routes.Remove(route);

        routeBuilder.MapRoute(
            "robots.txt",
            "robots.txt",
            new { controller = "MiscCommon", action = "RobotsTextFile" }
        );
    }
}

就这样。

在这个实现之后,路由工作正常,get 请求登陆我自己的控制器,就像原来的一样。

现在,我可以用我自己的替换生成逻辑。

我希望它有所帮助。

于 2018-07-05T10:47:05.050 回答
1

RouteProvider.cs你的插件中写下这些代码(根据你的名字):

var lastExistingRoute= routeBuilder.Routes.FirstOrDefault(x => ((Route)x).Name == "HomePage");
            routeBuilder.Routes.Remove(lastExistingRoute);
            routeBuilder.MapRoute("HomePage", "",
                new { controller = "CustomPage", action = "Homepage", });

以下代码适用于我自己的 4.20 版:

  var lastDownloadRoute=routeBuilder.Routes.FirstOrDefault(x => ((Route)x).Name == "GetDownload");
            routeBuilder.Routes.Remove(lastDownloadRoute);
            routeBuilder.MapRoute("GetDownload", "download/getdownload/{guid}/{agree?}",
                new { controller = "AzTechProduct", action = "GetPayed", });
于 2019-09-08T19:49:14.417 回答
1

通过快速检查代码,我发现在 nopCommerce 4.3 中有两种可能的方法来处理这个问题。

首先,您可以创建一个 IRouteProvider,添加与您希望“删除”的路由具有相同签名的路由,并确保提供程序的优先级大于 1。

这样做基本上会覆盖 Nop 中内置的默认路由。这是我的首选方法。

    public partial class RouteProvider: IRouteProvider
    {
        public void RegisterRoutes(IEndpointRouteBuilder endpointRouteBuilder)
        {

            var pattern = string.Empty;
            if (DataSettingsManager.DatabaseIsInstalled)
            {
                var localizationSettings = endpointRouteBuilder.ServiceProvider.GetRequiredService<LocalizationSettings>();
                if (localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
                {
                    var langservice = endpointRouteBuilder.ServiceProvider.GetRequiredService<ILanguageService>();
                    var languages = langservice.GetAllLanguages().ToList();
                    pattern = "{language:lang=" + languages.FirstOrDefault().UniqueSeoCode + "}/";
                }
            }

            // Handle the standard request
            endpointRouteBuilder.MapControllerRoute("Wishlist", pattern + "wishlist/{customerGuid?}",
                new { controller = "MyShoppingCart", action = "Wishlist" });

            return;
        }

        public int Priority => 100;
    }

上面代码的关键是 Priority 值。此路由将首先添加到列表中,因此优先于默认路由。使用此技术无需删除默认路由。

The second possible method turns out to not work because the endpointRouteBuilder.DataSources[n].Endpoints collection is read only. So, as far as I know, you can't remove mappings from that list after they have been added.

于 2021-03-24T18:35:21.373 回答