public ActionResult CityHandle(string city)
        {
            var cityService = EngineContext.Current.Resolve <ICityService>();
            var cityObj     = cityService.GetCityByUrl(city.ToLower());

            if (cityObj != null)
            {
                var keywordWithCity = new KeywordsMapping
                {
                    CityId = cityObj.Id
                };

                return(KeywordHandle(keywordWithCity));
            }

            return(RedirectToAction("Search", "Catalog"));
        }
 // GET: KeywordsMapping
 public ActionResult KeywordHandle(KeywordsMapping keyword)
 {
     return(RedirectToAction("Search", "Catalog"));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns information about the requested route.
        /// </summary>
        /// <param name="httpContext">An object that encapsulates information about the HTTP request.</param>
        /// <returns>
        /// An object that contains the values from the route definition.
        /// </returns>
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            RouteData data = base.GetRouteData(httpContext);

            if (data != null && DataSettingsHelper.DatabaseIsInstalled())
            {
                var urlRecordService = EngineContext.Current.Resolve <IUrlRecordService>();
                var slug             = (data.Values["generic_se_name"] as string).ToLower();

                if (slug == "catalog")
                {
                    data.Values["controller"] = "Catalog";
                    data.Values["action"]     = data.Values["city"] as string ?? "Search";
                    // data.Values["model"]
                    return(data);
                }
                else if (slug == "lojas")
                {
                    data.Values["controller"] = "KeywordsMapping";
                    data.Values["action"]     = "CityHandle";
                    data.Values["city"]       = data.Values["city"] as string ?? "";

                    return(data);
                }
                else if (slug == "pensediferente")
                {
                    data.Values["controller"] = "Catalog";
                    data.Values["action"]     = "Search";

                    return(data);
                }

                #region Keyword handler

                // Moveleiros
                // Check for keyword :)
                var keywordMappingService = EngineContext.Current.Resolve <IKeywordsMappingService>();
                var keyword = keywordMappingService.GetKeywordByNormalized(slug);

                if (keyword != null)
                {
                    var cityDesc = data.Values["city"] as string;

                    if (!string.IsNullOrEmpty(cityDesc))
                    {
                        // TODO: Add City
                    }

                    data.Values["controller"] = "KeywordsMapping";
                    data.Values["action"]     = "KeywordHandle";
                    data.Values["keyword"]    = keyword;

                    return(data);
                }

                // Check for cities :)
                var cityService = EngineContext.Current.Resolve <ICityService>();
                var city        = cityService.GetCityByUrl(slug);

                if (city != null)
                {
                    var keywordWithCity = new KeywordsMapping
                    {
                        CityId = city.Id
                    };

                    data.Values["controller"] = "KeywordsMapping";
                    data.Values["action"]     = "KeywordHandle";
                    data.Values["keyword"]    = keywordWithCity;

                    return(data);
                }

                #endregion

                //performance optimization.
                //we load a cached verion here. it reduces number of SQL requests for each page load
                var urlRecord = urlRecordService.GetBySlugCached(slug);
                //comment the line above and uncomment the line below in order to disable this performance "workaround"
                //var urlRecord = urlRecordService.GetBySlug(slug);
                if (urlRecord == null)
                {
                    //no URL record found

                    //var webHelper = EngineContext.Current.Resolve<IWebHelper>();
                    //var response = httpContext.Response;
                    //response.Status = "302 Found";
                    //response.RedirectLocation = webHelper.GetStoreLocation(false);
                    //response.End();
                    //return null;

                    data.Values["controller"] = "Common";
                    data.Values["action"]     = "PageNotFound";
                    return(data);
                }
                //ensure that URL record is active
                if (!urlRecord.IsActive)
                {
                    //URL record is not active. let's find the latest one
                    var activeSlug = urlRecordService.GetActiveSlug(urlRecord.EntityId, urlRecord.EntityName, urlRecord.LanguageId);
                    if (string.IsNullOrWhiteSpace(activeSlug))
                    {
                        //no active slug found

                        //var webHelper = EngineContext.Current.Resolve<IWebHelper>();
                        //var response = httpContext.Response;
                        //response.Status = "302 Found";
                        //response.RedirectLocation = webHelper.GetStoreLocation(false);
                        //response.End();
                        //return null;

                        data.Values["controller"] = "Common";
                        data.Values["action"]     = "PageNotFound";
                        return(data);
                    }

                    //the active one is found
                    var webHelper = EngineContext.Current.Resolve <IWebHelper>();
                    var response  = httpContext.Response;
                    response.Status           = "301 Moved Permanently";
                    response.RedirectLocation = string.Format("{0}{1}", webHelper.GetStoreLocation(), activeSlug);
                    response.End();
                    return(null);
                }

                //ensure that the slug is the same for the current language
                //otherwise, it can cause some issues when customers choose a new language but a slug stays the same
                var workContext            = EngineContext.Current.Resolve <IWorkContext>();
                var slugForCurrentLanguage = SeoExtensions.GetSeName(urlRecord.EntityId, urlRecord.EntityName, workContext.WorkingLanguage.Id);
                if (!String.IsNullOrEmpty(slugForCurrentLanguage) &&
                    !slugForCurrentLanguage.Equals(slug, StringComparison.InvariantCultureIgnoreCase))
                {
                    //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)
                    var webHelper = EngineContext.Current.Resolve <IWebHelper>();
                    var response  = httpContext.Response;
                    //response.Status = "302 Found";
                    response.Status           = "302 Moved Temporarily";
                    response.RedirectLocation = string.Format("{0}{1}", webHelper.GetStoreLocation(), slugForCurrentLanguage);
                    response.End();
                    return(null);
                }

                //process URL
                switch (urlRecord.EntityName.ToLowerInvariant())
                {
                case "product":
                {
                    data.Values["controller"] = "Product";
                    data.Values["action"]     = "ProductDetails";
                    data.Values["productid"]  = urlRecord.EntityId;
                    data.Values["SeName"]     = urlRecord.Slug;
                }
                break;

                case "category":
                {
                    data.Values["controller"] = "Catalog";
                    data.Values["action"]     = "Category";
                    data.Values["categoryid"] = urlRecord.EntityId;
                    data.Values["SeName"]     = urlRecord.Slug;
                }
                break;

                case "manufacturer":
                {
                    data.Values["controller"]     = "Catalog";
                    data.Values["action"]         = "Manufacturer";
                    data.Values["manufacturerid"] = urlRecord.EntityId;
                    data.Values["SeName"]         = urlRecord.Slug;
                }
                break;

                case "vendor":
                {
                    data.Values["controller"] = "Catalog";
                    data.Values["action"]     = "Vendor";
                    data.Values["vendorid"]   = urlRecord.EntityId;
                    data.Values["SeName"]     = urlRecord.Slug;
                }
                break;

                case "newsitem":
                {
                    data.Values["controller"] = "News";
                    data.Values["action"]     = "NewsItem";
                    data.Values["newsItemId"] = urlRecord.EntityId;
                    data.Values["SeName"]     = urlRecord.Slug;
                }
                break;

                case "blogpost":
                {
                    data.Values["controller"] = "Blog";
                    data.Values["action"]     = "BlogPost";
                    data.Values["blogPostId"] = urlRecord.EntityId;
                    data.Values["SeName"]     = urlRecord.Slug;
                }
                break;

                case "topic":
                {
                    data.Values["controller"] = "Topic";
                    data.Values["action"]     = "TopicDetails";
                    data.Values["topicId"]    = urlRecord.EntityId;
                    data.Values["SeName"]     = urlRecord.Slug;
                }
                break;

                default:
                {
                    //no record found

                    //generate an event this way developers could insert their own types
                    EngineContext.Current.Resolve <IEventPublisher>()
                    .Publish(new CustomUrlRecordEntityNameRequested(data, urlRecord));
                }
                break;
                }
            }
            return(data);
        }