返回首页 | 金赞娱乐场

合作共赢、快速高效、优质的网站建设提供商

更多精品源码-尽在织梦模板-www.moke8.com

nopCommerce 3.9 大波浪系列 之 路由注册

时间:2017-07-27 编辑:admin

GenericPathRoute对路由进行扩展,只重写了GetRouteData方法用于解析url,帮助路由到正确的执行位置。代码如下:

 1 /// summary 
 2 /// Returns information about the requested route.
 3 /// /summary 
 4 /// param name="httpContext" An object that encapsulates information about the HTTP request. /param 
 5 /// returns 
 6 /// An object that contains the values from the route definition.
 7 /// /returns 
 8 public override RouteData GetRouteData(HttpContextBase httpContext)
 10 RouteData data = base.GetRouteData(httpContext); return null;
 11 if (data != null DataSettingsHelper.DatabaseIsInstalled())
 12 {
 13 var urlRecordService = EngineContext.Current.Resolve IUrlRecordService 
 14 var slug = data.Values["generic_se_name"] as string;
 15 //performance optimization.
 16 //we load a cached verion here. it reduces number of SQL requests for each page load
 17 var urlRecord = urlRecordService.GetBySlugCached(slug);
 18 //comment the line above and uncomment the line below in order to disable this performance "workaround"
 19 //var urlRecord = urlRecordService.GetBySlug(slug);
 20 if (urlRecord == null)
 21 {
 22 //no URL record found
 24 //var webHelper = EngineContext.Current.Resolve IWebHelper 
 25 //var response = httpContext.Response;
 26 //response.Status = "302 Found";
 27 //response.RedirectLocation = webHelper.GetStoreLocation(false);
 28 //response.End();
 29 //return null;
 31 data.Values["controller"] = "Common";
 32 data.Values["action"] = "PageNotFound";
 33 return data;
 34 }
 35 //ensure that URL record is active
 36 if (!urlRecord.IsActive)
 37 {
 38 //URL record is not active. let's find the latest one
 39 var activeSlug = urlRecordService.GetActiveSlug(urlRecord.EntityId, urlRecord.EntityName, urlRecord.LanguageId);
 40 if (string.IsNullOrWhiteSpace(activeSlug))
 41 {
 42 //no active slug found
 44 //var webHelper = EngineContext.Current.Resolve IWebHelper 
 45 //var response = httpContext.Response;
 46 //response.Status = "302 Found";
 47 //response.RedirectLocation = webHelper.GetStoreLocation(false);
 48 //response.End();
 49 //return null;
 51 data.Values["controller"] = "Common";
 52 data.Values["action"] = "PageNotFound";
 53 return data;
 54 }
 56 //the active one is found
 57 var webHelper = EngineContext.Current.Resolve IWebHelper 
 58 var response = httpContext.Response;
 59 response.Status = "301 Moved Permanently";
 60 response.RedirectLocation = string.Format("{0}{1}", webHelper.GetStoreLocation(), activeSlug);
 61 response.End();
 62 return null;
 63 }
 65 //ensure that the slug is the same for the current language
 66 //otherwise, it can cause some issues when customers choose a new language but a slug stays the same
 67 var workContext = EngineContext.Current.Resolve IWorkContext 
 68 var slugForCurrentLanguage = SeoExtensions.GetSeName(urlRecord.EntityId, urlRecord.EntityName, workContext.WorkingLanguage.Id);
 69 if (!String.IsNullOrEmpty(slugForCurrentLanguage) 
 70 !slugForCurrentLanguage.Equals(slug, StringComparison.InvariantCultureIgnoreCase))
 71 {
 72 //we should make not null or "" validation above because some entities does not have SeName for standard (ID=0) language (e.g. news, blog posts)
 73 var webHelper = EngineContext.Current.Resolve IWebHelper 
 74 var response = httpContext.Response;
 75 //response.Status = "302 Found";
 76 response.Status = "302 Moved Temporarily";
 77 response.RedirectLocation = string.Format("{0}{1}", webHelper.GetStoreLocation(), slugForCurrentLanguage);
 78 response.End();
 79 return null;
 80 }
 82 //process URL
 83 switch (urlRecord.EntityName.ToLowerInvariant())
 84 {
 85 case "product":
 86 {
 87 data.Values["controller"] = "Product";
 88 data.Values["action"] = "ProductDetails";
 89 data.Values["productid"] = urlRecord.EntityId;
 90 data.Values["SeName"] = urlRecord.Slug;
 91 }
 92 break;
 93 case "category":
 94 {
 95 data.Values["controller"] = "Catalog";
 96 data.Values["action"] = "Category";
 97 data.Values["categoryid"] = urlRecord.EntityId;
 98 data.Values["SeName"] = urlRecord.Slug;
 99 }
100 break;
101 case "manufacturer":
102 {
103 data.Values["controller"] = "Catalog";
104 data.Values["action"] = "Manufacturer";
105 data.Values["manufacturerid"] = urlRecord.EntityId;
106 data.Values["SeName"] = urlRecord.Slug;
107 }
108 break;
109 case "vendor":
110 {
111 data.Values["controller"] = "Catalog";
112 data.Values["action"] = "Vendor";
113 data.Values["vendorid"] = urlRecord.EntityId;
114 data.Values["SeName"] = urlRecord.Slug;
115 }
116 break;
117 case "newsitem":
118 {
119 data.Values["controller"] = "News";
120 data.Values["action"] = "NewsItem";
121 data.Values["newsItemId"] = urlRecord.EntityId;
122 data.Values["SeName"] = urlRecord.Slug;
123 }
124 break;
125 case "blogpost":
126 {
127 data.Values["controller"] = "Blog";
128 data.Values["action"] = "BlogPost";
129 data.Values["blogPostId"] = urlRecord.EntityId;
130 data.Values["SeName"] = urlRecord.Slug;
131 }
132 break;
133 case "topic":
134 {
135 data.Values["controller"] = "Topic";
136 data.Values["action"] = "TopicDetails";
137 data.Values["topicId"] = urlRecord.EntityId;
138 data.Values["SeName"] = urlRecord.Slug;
139 }
140 break;
141 default:
142 {
143 //no record found
145 //generate an event this way developers could insert their own types
146 EngineContext.Current.Resolve IEventPublisher ()
147 .Publish(new CustomUrlRecordEntityNameRequested(data, urlRecord));
148 }
149 break;
150 }
151 }
152 return data;
153 }
GetRouteData

路由匹配到支持Seo友好链接会重定向链接新的路由位置,nop 3.9目前支持下图中的路由

提示:数据库UrlRecord表对Seo友情链接提供支持。

1.继承IRouteProvider对路由进行扩展。(注意路由注册顺序,ASP.NET MVC 只匹配第一次成功的路由信息)

2.多语言Seo友好链接通过LocalizedRoute类进行路由扩展(MapLocalizedRoute扩展中调用)

3.页面Seo友好链接通过GenericPathRoute类进行路由扩展(MapGenericPathRoute扩展中调用)

文中有错误的理解和不正确的观点,请留言,一起交流共同进步。


浏览:

网站建设

流程

    网站建设流程