Hi,
My problems are :
a. When I clicked on any product it is not redirected to product controller.
b. I have some customized filter options like sort by Author name, sort by product types ( book ,jwellery etc. these are specifications of individual products) for categories. These filter options are checkbox control when I select any check box , I did not get categoryid in return, due to which my action method failed to execute.
I did following things and it works for categories to call my customized action method.
1. I created class: BrowseGenericPathRoute.cs in my plugin
public class BrowseGenericPathRoute : GenericPathRoute
{
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;
//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;
}
//ensre 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))
{
//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(false), activeSlug);
response.End();
return null;
}
else
{
//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;
}
}
//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(false), slugForCurrentLanguage);
response.End();
return null;
}
//process URL
if(urlRecord.EntityName.ToLowerInvariant()== "category")
{
data.Values["controller"] = "WidgetsBrowse";
data.Values["action"] = "categoryNew";
data.Values["categoryid"] = urlRecord.EntityId;
data.Values["SeName"] = urlRecord.Slug;
}
}
return data;
}
}
2. Implemented IRouteProvider
public class RouteProvider : IRouteProvider
{
public void RegisterRoutes(RouteCollection routes)
{
routes.CustomGenericPathRoute("Plugin.Misc.Test.GenericUrl",
"{generic_se_name}",
new { controller = "Catalog", action = "Category" },
new[] { "Nop.Plugin.Widgets.WidgetsBrowse.Controllers" });
}
public int Priority
{
get
{
return -1;
}
}
}
3. I Created class BrowseGenericPathRouteExt.cs in my plugin to call CustomGenericPathRoute method mentioned in second step.
public static class BrowseGenericPathRouteExt
{
public static Route CustomGenericPathRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces)
{
if (routes == null)
{
throw new ArgumentNullException("routes");
}
if (url == null)
{
throw new ArgumentNullException("url");
}
var route = new BrowseGenericPathRoute(url, new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(defaults),
// Constraints = new RouteValueDictionary(constraints),
DataTokens = new RouteValueDictionary()
};
if ((namespaces != null) && (namespaces.Length > 0))
{
route.DataTokens["Namespaces"] = namespaces;
}
routes.Add(name, route);
return route;
}
}