Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
This topic describes routing and URL rewriting in Episerver. The URL (Uniform Resource Locator), also known as a web address, is a character string reference to a resource. In most web browsers, the URL displays at the top of a web page, inside an address bar. URLs carry information from the browser to the server required to enact a desired action. Routing rewrites a URL to make it more user-friendly.
A rewritten URL, also known as Friendly or Search Engine Friendly (SEF) URL, provides shorter and more relevant-looking links to web pages. By default, the routing system in Episerver uses System.Web.Routing, with specific segments added for language, node, and a partial route. Routing is automatically handled based on content type. The language segment will set the language of the page, and the node segment will set the page reference from the URL.
The technique adds a degree of separation between the files used to generate a web page and the URL that is presented. The partial route allows you to set the routed data to something else than PageData, for example catalog content in a Commerce solution. The registration of such a partial route is usually done in an initialization module.
There are several routes registered by default.
The routes are registered in the order above. The order is important because the first route checks whether the URL matches its route, and if it does, the page is routed through that route. If the URL does not match the first route, the next route gets its chance.
You can route a URL through the routing framework for both MVC and Web Forms. The routing first locates the content routed to from the URL. After the content is located, the framework queries the EPiServer.Web.TemplateResolver instance for the template that should be used to render the request. The template can be either a MVC controller or a Web Form. Depending on if the template is a Web Form or a MVC controller, a suitable httpHandler is set to handle the request. If no content is found that matches the URL or if no template is found that matches the routed content, a 404 (not found) is returned.
You can extend routing in several levels. There are events exposed both during incoming routing and creation of outgoing URLs to customize routing. You also can modify the default URL pattern for content routing to handle a part of the URL. You also can add your own routes.
The EPiServer.Web.Routing.ContentRoute class (responsible for routing to content) exposes the static events RoutingContent and RoutedContent, which are raised during incoming routing. RoutingContent events are raised before executing the default routing implementation, and the content that matches the request is set in an event handler. RoutedContent events are raised after executing the default routing, and the routed content is replaced in an event handler. During outgoing URL generation, the CreatingVirtualPath and CreatedVirtualPath events are raised where event handlers can modify the generated URLs.
The built-in content routing bases on a URL pattern that is registered, where each pattern is handled by an implementation of an EPiServer.Web.Routing.Segments.ISegment interface. You can implement custom ISegment types and register them as part of the content routing.
By default, content routing is registered with a pattern of {language}/{node}/{partial}/{action}.
The MapContentRoute extension method takes the name of the route, the URL pattern, and default values as arguments. Each part in the URL pattern for content routing (for example {node} or {partial}) is handled by an implementation of EPiServer.Web.Routing.Segments.ISegment. You can extend content routing with own ISegment implementations.
The following example shows how to add a route:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using EPiServer.Web.Routing;
namespace CodeSamples
{
/// <summary>
/// Code sample class containing cref examples on how to register a route
/// </summary>
public static class RegisterCustomRouteExample
{
/// <summary>
/// Register a route which makes 'shop/' in the beginning of the url valid.
/// This method should only be called when the site starts, for example in 'Application_Start' (Global.asax).
/// </summary>
public static void RegisterShopRoute()
{
// Register a route, which will make all url:s with 'shop/' before page names route to the page last in the list of page names
// For example, http://mySite/shop/News/ListOfNews/FirstNews/ will route to the 'FirstNews' page.
RouteTable.Routes.MapContentRoute(name: "customRoute",
url: "shop/{node}/{partial}/{action}",
defaults: new { action = "index" },
contentRootResolver: (s) => s.StartPage);
}
}
}
You also can add custom route implementations. The order of the routes are important because the first route handling a request prevents the following routes from routing the request. So when you register custom routes, decide whether it should be registered before or after the default routes.
ASP.NET has multiple checks that validate the length of the URL and the path in Windows. Under normal circumstances, the default settings are enough, but to support very long URL's, add the following configuration:
<httpRuntime maxUrlLength="1024" relaxedUrlToFileSystemMapping="true"/>
Last updated: Sep 21, 2015