Configure routing for controller actions

With Sitefinity CMS, you can work with attribute routing in the context of Sitefinity CMS widgets. As a result of the integration with ASP.NET MVC 5, you can use the RelativeRoute attribute to work with attribute routing and attributes to define routes for controller actions.

NOTE: When using the RelativeRoute attribute on one of the controllers actions, all controller actions must be decorated with the attribute, as well. 

RelativeRoute attribute

The RelativeRoute attribute has the same constructors and properties as the stock Route attribute. Sitefinity CMS automatically maps all routes defined, using the attributes for both RelativeRoute and Route attributes. As a result, you do not need to call the MapMvcAttributeRoutes.

The Route attribute, describes routes relative to the application path. The RelativeRoute attribute, on the other hand, describes routes relative to the page node where the widget is placed.

This example illustrates the difference between the two attributes. You have the following widget implementation:

If you add this widget to a page with an URL ~/my-page, then you get two routes:

  • ~/my-page/my-sample-path
    Displays the page and renders "This is Action1" on the place where the widget is located on the page.
  • ~/my-sample-path
    Renders "This is Action2" as plain text.

NOTE: If there are any RelativeRoute attributes on your controller, then the default Sitefinity CMS autorouting handling and the convention-based routes are ignored.
For more information, see For developers: Autorouting in pure and hybrid mode and Implement Master Detail content controllers.

You can also read the following tutorial about working with attribute routing: Create a Book widget

Built-in user-friendly routing

The out of the box Sitefinity CMS MVC widgets enable you filter the displayed content or change the paging via user-friendly routing options. For example, if you have a news widget on your page, you can directly type a category name, appending it to the page URL in the browser, and Sitefinity CMS filter the list of news and display only the ones marked with the specified taxonomy. Here are a few more examples:

  • www.mysite.com/NewsPage/ - this is the example URL of a page where your news widget is placed. This URL returns the default list of news items, as per your widget configuration.
  • www.mysite.com /NewsPage/2 - this URL returns the second page of news items (if  you have enabled paging for your widget. Passing this parameter in the URL is equivalent to pressing the "Next" or "2" buttons of the widget pager.
  • www.mysite.com /NewsPage/myTag - this URL instructs the widgets to display news items that are marked with a tag named “myTag”.
  • www.mysite.com /NewsPage/myCategory - this URL instructs the widgets to display news items that are marked with a category named “myCategory”.
  • www.mysite.com /NewsPage/myCategory/2 - this URL instructs the widgets to display the second page (if paging is enabled, and there are enough items to page through) of news items that are marked with a category named “myCategory”.

Specify explicitly the name of the built-in route parameter

It some of the cases you may want to specified explicitly the name of the route parameter you are filtering by in the URL, for example category, tag, page. By default, this behavior is disabled, and these route parameters are resolved automatically, without the need to explicitly specify their name. For example, if you specify www.mysite.com /NewsPage/category/myCategory, by default Sitefinity CMS will not resolve this URL, as the /category parameter name is specified explicitly.  You can instruct Sitefinity CMS to resolve explicitly named parameters. To do this, follow these steps:

  1. Go to Sitefinity CMS backend and navigate to Administration » Settings » Advanced
  2. From the left-hand navigation select Feather
  3. Select the UseNamedParametersRouting checkbox.
  4. Click on Save changes to persist your choise

RESULT: As a result, all out of the box MVC widgets will resolve the following routes:

  • www.mysite.com/NewsPage/ - this is the example URL of a page where your news widget is placed. This URL returns the default list of news items, as per your widget configuration.
  • www.mysite.com /NewsPage/page/2 - this URL returns the second page of news items (if  you have enabled paging for your widget. Passing this parameter in the URL is equivalent to pressing the "Next" or "2" buttons of the widget pager.
  • www.mysite.com /NewsPage/tag/myTag - this URL instructs the widgets to display news items that are marked with a tag named “myTag”.
  • www.mysite.com /NewsPage/category/myCategory - this URL instructs the widgets to display news items that are marked with a category named “myCategory”.
  • www.mysite.com /NewsPage/category/myCategory/page/2 - this URL instructs the widgets to display the second page (if paging is enabled, and there are enough items to page through) of news items that are marked with a category named “myCategory”.

 

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?