• NOP Commerce中的URL路由


    我想弄清楚它是如何工作的,例如,如果我拉起一个类别页面:http:// localhost:15536​​ / desktops,它将在CatalogController中命中以下Action方法:

    public virtual IActionResult Category(int categoryId, CatalogPagingFilteringModel command)

    那么如何传递正确的类别ID(int),但它不是URL的一部分,它如何工作?

    在Nopcommerce中,有一个名为的实体UrlRecord,您可以在Nop.Core \ Domain \ Seo \ UrlRecord.cs中找到它:

    public partial class UrlRecord : BaseEntity
    {
        public int EntityId { get; set; }
    
        public string EntityName { get; set; }
    
        public string Slug { get; set; }
    
        public bool IsActive { get; set; }
    
        public int LanguageId { get; set; }
    }
    

    EntityName,表明这UrlRecord是使用针对实体(IG产品或类别,等等)。EntityId指示Id尖实体(IG IdProduct)。Slug表明什么网址可以达到预期的实体。在您的示例中,EntityName“类别” EntityIdId该类别的和Slug是“桌面”。

    那么,Nopcommerce如何将这些路由Slug到正确的Controller and Action?为了弄清楚这一点,我们必须查看GenericPathRoute位于Nop.Web.Framework \ Seo \ GenericPathRoute.cs中的类。这个类被注册为一个自定义RouteIRouteBuilder无论asp.net核心路由和Nopcommerce详细信息如何,在每次请求开始时都会调用RouteAsync方法GenericPathRoute如果我们研究这种方法,我们可以看到本节(在本节之前,urlRecord是从那里从数据库中提取的Slug,因此我们知道什么是实体及其实体Id,因此我们可以将其正确地路由到所需的Controller and Action上。参数):

    var currentRouteData = new RouteData(context.RouteData);
    switch (urlRecord.EntityName.ToLowerInvariant())
    {
        case "product":
            currentRouteData.Values[NopPathRouteDefaults.ControllerFieldKey] = "Product";
            currentRouteData.Values[NopPathRouteDefaults.ActionFieldKey] = "ProductDetails";
            currentRouteData.Values[NopPathRouteDefaults.ProductIdFieldKey] = urlRecord.EntityId;
            currentRouteData.Values[NopPathRouteDefaults.SeNameFieldKey] = urlRecord.Slug;
            break;
        case "producttag":
            currentRouteData.Values[NopPathRouteDefaults.ControllerFieldKey] = "Catalog";
            currentRouteData.Values[NopPathRouteDefaults.ActionFieldKey] = "ProductsByTag";
            currentRouteData.Values[NopPathRouteDefaults.ProducttagIdFieldKey] = urlRecord.EntityId;
            currentRouteData.Values[NopPathRouteDefaults.SeNameFieldKey] = urlRecord.Slug;
            break;
        case "category":
            currentRouteData.Values[NopPathRouteDefaults.ControllerFieldKey] = "Catalog";
            currentRouteData.Values[NopPathRouteDefaults.ActionFieldKey] = "Category";
            currentRouteData.Values[NopPathRouteDefaults.CategoryIdFieldKey] = urlRecord.EntityId;
            currentRouteData.Values[NopPathRouteDefaults.SeNameFieldKey] = urlRecord.Slug;
            break;
        case "manufacturer":
            currentRouteData.Values[NopPathRouteDefaults.ControllerFieldKey] = "Catalog";
            currentRouteData.Values[NopPathRouteDefaults.ActionFieldKey] = "Manufacturer";
            currentRouteData.Values[NopPathRouteDefaults.ManufacturerIdFieldKey] = urlRecord.EntityId;
            currentRouteData.Values[NopPathRouteDefaults.SeNameFieldKey] = urlRecord.Slug;
            break;
        case "vendor":
            currentRouteData.Values[NopPathRouteDefaults.ControllerFieldKey] = "Catalog";
            currentRouteData.Values[NopPathRouteDefaults.ActionFieldKey] = "Vendor";
            currentRouteData.Values[NopPathRouteDefaults.VendorIdFieldKey] = urlRecord.EntityId;
            currentRouteData.Values[NopPathRouteDefaults.SeNameFieldKey] = urlRecord.Slug;
            break;
        case "newsitem":
            currentRouteData.Values[NopPathRouteDefaults.ControllerFieldKey] = "News";
            currentRouteData.Values[NopPathRouteDefaults.ActionFieldKey] = "NewsItem";
            currentRouteData.Values[NopPathRouteDefaults.NewsItemIdFieldKey] = urlRecord.EntityId;
            currentRouteData.Values[NopPathRouteDefaults.SeNameFieldKey] = urlRecord.Slug;
            break;
        case "blogpost":
            currentRouteData.Values[NopPathRouteDefaults.ControllerFieldKey] = "Blog";
            currentRouteData.Values[NopPathRouteDefaults.ActionFieldKey] = "BlogPost";
            currentRouteData.Values[NopPathRouteDefaults.BlogPostIdFieldKey] = urlRecord.EntityId;
            currentRouteData.Values[NopPathRouteDefaults.SeNameFieldKey] = urlRecord.Slug;
            break;
        case "topic":
            currentRouteData.Values[NopPathRouteDefaults.ControllerFieldKey] = "Topic";
            currentRouteData.Values[NopPathRouteDefaults.ActionFieldKey] = "TopicDetails";
            currentRouteData.Values[NopPathRouteDefaults.TopicIdFieldKey] = urlRecord.EntityId;
            currentRouteData.Values[NopPathRouteDefaults.SeNameFieldKey] = urlRecord.Slug;
            break;
        default:
            //no record found, thus generate an event this way developers could insert their own types
            EngineContext.Current.Resolve<IEventPublisher>()
                ?.Publish(new CustomUrlRecordEntityNameRequestedEvent(currentRouteData, urlRecord));
            break;
    }
    context.RouteData = currentRouteData;
    

    我们可以在这里看到它更改了此请求的routeData,关于EntityName我们在这里可以看到,此功能仅对在此switch / case命令中命名的实体有效。

  • 相关阅读:
    关于because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified报错的解决方案
    对不起,这是一篇负能量爆棚的文章
    selenium启动报错“ incorrect JSON status mapping for 'unknown error' (500 expected)”
    解决关于win10下eclipse代码格式化不生效问题
    递归遍历所有xml的节点及子节点
    性能调优-CPU方面,内存方面
    二进制日志备份与恢复,快照备份,复制
    逻辑备份,mysqldump,SELECT…INTO OUTFILE,恢复
    备份与恢复概述,冷备,热备
    分布式事务,不好的事务习惯
  • 原文地址:https://www.cnblogs.com/Alex80/p/16091464.html
Copyright © 2020-2023  润新知