Popular blog tags

NopCommerce 4.x -Register new routes in NopCommerce

Published

NopCommerce 4.x -Register new routes in NopCommerce

NopCommerce version:NopCommerce 4.61
.Net SDK version:Net 7.x

step 1.program.cs

app.MapControllerRoute(
    name: "people",
    pattern: "people/{ssn}",
    constraints: new { ssn = "^\\d{3}-\\d{2}-\\d{4}$", },
    defaults: new { controller = "People", action = "List" });

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-7.0

 

step 2. extent with static class

program.cs

app.MapModuleRoute3();  //static Routing
namespace Microsoft.AspNetCore.Builder
{
    public static class RegisterRoutes3
    {

        /// <summary>
        /// 注册静态路由
        /// come from;https://github.com/nopSolutions/nopCommerce/blob/12dd4825dfb56bbc64e192cd35d25e3205427646/src/Presentation/Nop.Web/Infrastructure/RouteProvider.cs   nop4.4
        /// </summary>
        /// <param name="routes"></param>
        public static void MapModuleRoute3(this IEndpointRouteBuilder endpointRouteBuilder)
        {


          
            //custom article RSS
            endpointRouteBuilder.MapControllerRoute("ArticleRSS", "article/rss/{languageId:min(0)}",
                new { controller = "Article", action = "ListRss" });



            //areas
          //  endpointRouteBuilder.MapControllerRoute(name: "areaRoute", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

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

            // 1. for old
            endpointRouteBuilder.MapControllerRoute("OldBlogPost", "/blog/blogpost/{id?}/{SeName?}", new { controller = "Blog", action = "BlogPost" });

            //2.
            // endpointRouteBuilder.MapControllerRoute("BlogPost", "/blog/{id:length(24)}/{SeName}", new { controller = "Blog", action = "BlogPost" });

            //3. https://www.iaspnetcore.com/how-to-port-a-windows-forms-desktop-app-to-net-6x?id=6082e77e788a1b4f087ea7ed  login等都不行
            //  endpointRouteBuilder.MapControllerRoute("CustomBlogPost1", "{SeName}", new { controller = "Blog", action = "BlogPost" });
           
            

            // 4. for search
            endpointRouteBuilder.MapControllerRoute("BlogPost", "blogpost-{id}-{SeName}", new { controller = "Blog", action = "BlogPost" });

            //news
            endpointRouteBuilder.MapControllerRoute("NewsItem", "News/NewsItem/{Id?}/{SeName?}", new { controller = "News", action = "NewsItem" });

            endpointRouteBuilder.MapControllerRoute("customTopic", "Topic/TopicDetails/{topicId?}/{SeName?}", new { controller = "Topic", action = "topicdetail " });

            #endregion custom extend end

        }
    }
}

step 3. day class

nopCommerce/src/Presentation/Nop.Web/Infrastructure/RouteProvider.cs 

using Nop.Services.Installation;
using Nop.Web.Framework.Mvc.Routing;

namespace Nop.Web.Infrastructure
{
    /// <summary>
    /// Represents provider that provided basic routes
    /// </summary>
    public partial class RouteProvider : BaseRouteProvider, IRouteProvider
    {
        #region Methods

        /// <summary>
        /// Register routes
        /// </summary>
        /// <param name="endpointRouteBuilder">Route builder</param>
        public void RegisterRoutes(IEndpointRouteBuilder endpointRouteBuilder)
        {
        
            //home page
            endpointRouteBuilder.MapControllerRoute(name: "Homepage",
                pattern: $"{lang}",
                defaults: new { controller = "Home", action = "Index" });

            //login
            endpointRouteBuilder.MapControllerRoute(name: "Login",
                pattern: $"{lang}/login/",
                defaults: new { controller = "Customer", action = "Login" });
...

        }
    }
}

step 4.call RegisterRoutes()