Search code examples
asp.net-mvcasp.net-mvc-routing

Make advanced MVC Route


I’ve a website that contains two main dynamic modules:

1- Module (1): To able the website admin from managing the whole website pages’ static content, he is able from adding a parent pages and child up to 2 levels

2- Module (2): To able the website admin from managing the news’ articles that should be published to the website

For the routing, we’re planning to make it as following:

1- Module (1)

www.websitename.com/parentpage

www.websitename.com/parentpage/childpage

www.websitename.com/parentpage/childpage/childchildpage

2- Module (2)

www.websitename.com/news/newstitle

The problem here is module(2) routing not handled or executed never. I think because its conflicts here with the second scenario in module(1).

Any suggestions or recommendations?

Best Regards,

shady


Solution

  • Thank you for your help. I have solved this problem using RouteBase

     public class MyUrlRoute : RouteBase
    {
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            //Route Code
        }
    
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            //Virtual Path Code
        }
    }
    

    In RouteConfig

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(new MyUrlRoute()); // Add before your default Routes
        defaults: new { language = "en", controller = "Home", action = "Home" },
        namespaces: new string[] { "Solution.Website.Controllers" }
        );
    }