Home > Backend Development > C++ > How Can Custom Routing in MVC Handle Multi-Level Dynamic URL Paths?

How Can Custom Routing in MVC Handle Multi-Level Dynamic URL Paths?

Barbara Streisand
Release: 2025-01-29 09:51:14
Original
375 people have browsed it

How Can Custom Routing in MVC Handle Multi-Level Dynamic URL Paths?

Multiple Levels in MVC Custom Routing

Problem:

In the context of building a custom content management system (CMS), the need arises for dynamic URL path structures that allow an administrator to define custom path levels, such as "newslocalmynewdog" or "Articleseventsconventionsmycon."

Solution:

Custom RouteBase Subclass:

To achieve this custom routing scenario, creating a custom RouteBase subclass, such as CustomPageRoute, is essential. This class defines the logic for determining whether a request matches a specific route and generates the corresponding URL path.

Matching Logic:

The GetRouteData method in CustomPageRoute is responsible for matching incoming requests to CMS-style paths. It extracts the virtual path from the request URL and attempts to match it against a list of known paths stored in the cache.

Generating URLs:

The GetVirtualPath method generates URL paths for specific controller actions and route values. It uses the same matching logic as in GetRouteData and returns the virtual path that matches the request parameters.

Route Registration:

Once the CustomPageRoute class is defined, it can be registered with the MVC routing system using the routes.Add method. The MapRoute method is used for registering a default route to handle all other requests not matched by the custom route.

Controller and Action:

The custom route expects a controller named CustomPage with an action method named Details to handle the matched requests. The Details method can retrieve the page data corresponding to the route parameters and display the appropriate content.

Example Code:

// Custom RouteBase Subclass
public class CustomPageRoute : RouteBase
{
    // Route matching logic in GetRouteData
    // URL generation logic in GetVirtualPath
}

// Route Registration
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
routes.Add(name: "CustomPage", item: new CustomPageRoute());

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

// Custom Page Controller
public class CustomPageController : Controller
{
    public ActionResult Details(Guid id)
    {
        // Retrieve page data and display content
        return View();
    }
}
Copy after login

The above is the detailed content of How Can Custom Routing in MVC Handle Multi-Level Dynamic URL Paths?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template