MVC Url Pattern

Carsten Gerwing asked on January 11, 2019 16:58

Hi everyone,

is it possible to use the NodeAliasPath as the url pattern in a mvc application? Somehow following setup is not working with the NodeAliasPath and the DocumentNamePath as well:

routes.MapRoute(
           name: "Default",
           url: "{ NodeAliasPath }",
           defaults: new { controller = "universal", action = "Index", id = UrlParameter.Optional           });

It's working with the NodeID and other patterns, but not this. The pattern works fine in the backend/Page applications?

Any suggestions? It would be greatly appreciated as I need this for a custom solution.

Regards Carsten

Correct Answer

Trevor Fayas answered on January 28, 2019 16:09

Here you go! The guide and sample files on how to route by NodeAliasPath / Url Alias like Portal does.

http://www.devtrev.com/Trev-Tips-(Blog)/January-2019/Dynamic-Routing-with-Kentico-MVC

1 votesVote for this answer Unmark Correct answer

Recent Answers


Zach Perry answered on January 11, 2019 18:37

You're not passing the NodeAliasPath paramater you are creating into anything.

A normal routing is set up something like this:

 routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

Not sure how your controller is set up but I would imagine you would need to do something like this:

routes.MapRoute(
    name: "Default",
     url: "{ NodeAliasPath }",
    defaults: new { controller = "universal", action = "Index", NodeAliasPath = UrlParameter.Optional }
);

This will pass the entire URL into your controller.

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on January 11, 2019 18:44

How does your "universal" controller retrieve page content?

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on January 11, 2019 21:37

I am still testing this out, but i believe you can create a catch-all route at the end (that if nothing else matches, run it) which can then take a look at the current url, do a lookup on the Nodes to grab the matching TreeNode, and based on it's page type/class (which should have a Url pattern in the page type of that class) you can redirect to the proper controller.

I could be wrong, that was my understanding, i am testing that out next once i get the widgets going.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on January 22, 2019 18:37

Just to give an update, i have created this exact thing, took a custom RouteHandler to catch a "Control Not Found" and do a lookup on the NodeAliasPath, and then dynamically set what Controller should handle the request. I have the code and will be writing a blog (devtrev.com) about it end of this month, if you want contact me at tfayas@gmail.com and i can get it to you early!

0 votesVote for this answer Mark as a Correct answer

Mark Clark answered on February 23, 2019 00:06

I was looking everywhere for this. This seems like important, fundamental CMS functionality to allow content editors to be able to create arbitrary pages at the root level. Why isn't this covered by the official doc/tutorials?

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.