MapRoute for Razor Pages

In ASP.NET MVC, we can map URLs to controllers, actions, and parameters. For example, the following MapRoute is used by default in an ASP.NET MVC application.

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

This means that if we have url / value1 / value2 / value3, ASP.NET MVC will trigger the Value2 action of the Value1 controller, and Value3 will be used for the id variable. I want to create something similar with Razor Pages.

My scenario is too complicated, so I will try to explain it with a simple example. For example, I will have a page called "/Pages/Article.cshtml" with the following definition at the top of the page

@page "{articlefriendlyname}"

This means that I can call up the article page using the following format

/Articles/do-something-article

In C # code, I will have the following OnGet method:

public void OnGet(string articlefriendlyname)
{

}

I can now have the following URLs working in my web application

/Article/do-something
/Articles/do-something-else

, URL- , URL-:

/do-something
/do-something-else

, URL:

/{articlefriendlyname}      ==>     /Article/{articlefriendlyname}

index.cshtml , Articles URL-, - - , .

, , , .

https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/razor-pages-convention-features

+4

Source: https://habr.com/ru/post/1692169/


All Articles