4

I am trying to create a plugin which will override the TopicsDetails.cshtml page. I added a route like this:

      routes.MapRoute("Nop.Plugin.Other.CustomTopic.ViewCustomTopic", "{SeName}",
                        new { controller = "CustomTopic", action = "TopicDetails", SeName = UrlParameter.Optional },
                        new[] { "Nop.Plugin.Other.CustomTopic.Controllers" });

This is getting all the {SeName} to my CustomTopicController .Even the products SeName.

If I add this instead of the older one:

       routes.MapRoute("Nop.Plugin.Other.CustomTopic.ViewCustomTopic",
                        new { controller = "CustomTopic", action = "TopicDetails" },
                        new[] { "Nop.Plugin.Other.CustomTopic.Controllers" });

I get an error because the TopicDetails(int itemId) Action receives an integer which is not provided as we know that GenericPathRoutes.cs Provides that integer.

How can I override the Rules of GenericPathRoutes.cs to do it so that only the topic SeName would hit my Controller or is there other way to do that kind of work or is it even possible to do?

Sujit
  • 790
  • 1
  • 13
  • 27

2 Answers2

1

Recently i wrote an article which shows how to override localized route and i used the TopicDetails view as an example. There article is here. Inshort, your route provider shall look like this;

public class RouteProvider : IRouteProvider
{
    private const string NAMESPACES = "Nop.Plugin.Misc.Custom.Controllers";
    private const string CONTROLLER = "MiscCustom";

    public void RegisterRoutes(RouteCollection routes)
    {     
        //Public Override
        routes.MapGenericPathRoute("Plugin.Misc.Custom.GenericUrl",
            "{generic_se_name}",
            new { controller = "Common", action = "GenericUrl" },
            new[] { NAMESPACES });

    }

    public int Priority
    {
        get { return Int32.Max; }
    }
}
Idrees Khan
  • 7,702
  • 18
  • 63
  • 111
  • 1
    This works like a charm. May I ask you something related to your sample plugin? In the article, you define the priority as -1 but when i download the source code there you have int.max? which one is better? I thought that it should more than 0 in order to override default controller. thanks – Emil Oct 27 '15 at 15:12
0

I am not sure whether i understand your question can you try it by adding id = UrlParameter.Optional here any id parameter is optional it wont throw that error

MustangManiac
  • 317
  • 2
  • 13
  • actually the default route in nopcommerce 3.3 is like that: `routes.MapLocalizedRoute("Topic", "{SeName}", new { controller = "Topic", action = "TopicDetails" }, new[] { "Nop.Web.Controllers" });` and it should give the error. Because TopicDetails(int topicid) method will accept an integer.But there is no error in default because an integer (topicid) is generated by the Nop.Web.Framework.Seo.GenericPathRoute.cs. Now how can i override the existing GenericPathRoute.cs in my plugin?? – Sujit May 22 '14 at 12:48