Home  >  Article  >  Web Front-end  >  A detailed introduction to ASP.NET Core Razor page routing

A detailed introduction to ASP.NET Core Razor page routing

巴扎黑
巴扎黑Original
2017-09-05 11:05:172119browse

This article mainly introduces the detailed explanation of ASP.NET Core Razor page routing. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look

In the server-side Web application framework, a very important design is how developers match URLs with resources on the server in order to correctly handle requests. The simplest way is to map the URL to a physical file on disk, which is how the ASP.NET team implements it in the Razor Pages framework.

There are some rules you must know about how the Razor Pages framework matches URLs to files, and how you can customize the rules to change the output as needed. If you are comparing Razor Pages to the Web Forms framework, you also need to understand the substituted URL parameters and the mechanism for passing data in the URL.

Rule 1, Razor pages need a root directory. By default, this root directory is Pages, located in the root directory of the web application project. You can configure other folders as the root directory in the ConfigureServices method of the Startup class. The following is to change the root directory to be located in the application "Content" folder:


 public void ConfigureServices(IServiceCollection services)
 { 
  services 
   .AddMvc(). 
   AddRazorPagesOptions(options => { 
    options.RootDirectory = "/Content";
   }); 
 }

Rule two, the URL is mapped to the Razor page, the URL does not contain the file extension .

Rule 3, "Index.cshtml" is a default document, which means that if

URL maps the file
www.domain.com /Pages/Index.cshtml
www.domain .com/index /Pages/Index.cshtml
www.domain.com/index /Pages/Index.cshtml
www.domain.com/account /Pages/account.cshtml or /Pages/account/index.cshtml

在最后一个例子中,URL映射到两个不同的文件 - 根目录中的“account.cshtml”、“account”文件夹中的“index.cshtml”。Razor 页面框架无法识别要选择哪一个文件,因此如果您在应用程序中实际同时拥有这两个文件,那么如果您尝试浏览www.domain.com/account,会抛出如下异常:

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

Page: /account/Index

Page: /account

URL传递参数

就像大多数其它框架一样,参数可以作为查询字符串在 URL 中传递,例如:www.domain.com/product?id=1;或者,您可以将其作为路由参数传递,因此上述示例将变为www.domain.com/product/1。URL的一部分必须映射到参数名称,在页面的路由模板来实现的,@page指令的一部分:


@page "{id}"

该模板告诉框架将页面名称之后URL的第一段作为“id”的路由参数。您可以通过多种方式访问路由参数的值。第一个是使用RouteData字典:


@page "{id}"
{
 var productId = RouteData.Values["id"];
}

或者,您可以向该页面的OnGet()方法添加与路由参数相同名称的参数,并将其值分配给公共属性:


@page "{id}"
@{
 @functions{

  public int Id { get; set; }

  public void OnGet(int id)
  {
   Id = id;
  }
 }
}

The Id is @Id

如果您使用的是PageModel,那么是这样实现的:


using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RazorPages.Pages
{
 public class ProductModel : PageModel
 {
  public int Id { get; set; }
  public void OnGet(int id)
  {
   Id = id;
  }
 }
}


@page "{id}"
@model ProductModel

The Id is @Model.Id

最后,您可以在公有属性使用BindProperty特性,并省略该OnGet方法中的参数。Razor 文件内容保持不变,但是PageModel代码略有更改:


using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RazorPages.Pages
{
 public class ProductModel : PageModel
 {
  [BindProperty(SupportsGet = true)]
  public int Id { get; set; }
  public void OnGet()
  {
  }
 }
}

约束

此外,在此示例中参数的约束是它必须有一个值。URL www.domain.com/product/applewww.domain.com/product/21一样有效,都是可以与路由匹配。如果您希望id值为整数,则可以通过将数据类型添加到模板来指定约束:


@page "{id:int}"

现在,如果您尝试通过“apple”作为参数值,应用程序将返回404 Not Found状态码。

您可以指定值不是必需的,可以将参数设置为可为空类型:


@page "{id:int?}"

如果您的应用程序允许使用“apple”作为参数值,则可以指定只允许使用A-Z和a-z的字符:


@page "{id:alpha}"

您可以与最小长度要求相结合:


@page "{id:alpha:minlength(4)}"

更多的约束信息,可以查看微软文档。

友好URL

友好的URL能够将 URL 映射到磁盘上的任意文件,打破根据文件名一对一的映射关系。您可以使用这个特性来不改变 URL 以进行SEO优化而不能重命名文件的问题,例如,如果希望所有请求由一个文件进行处理。友好 URL 在Startup类型的ConfigureServices方法中配置,调用RazorPagesOption类的AddPageRoute方法。以下示例将 URL www.domain.com/product 映射到Razor 页面 “extras”文件夹“products.cshtml”文件:


 public void ConfigureServices(IServiceCollection services)
 {
  services
   .AddMvc()
   .AddRazorPagesOptions(options =>
   {
    options.Conventions.AddPageRoute("/extras/products", "product");
   });
 }

如果您在 Web Forms 中使用过友好 URL,则应注意AddPageRoute方法的参数顺序与 Web Forms MapPageRoute方法相反,文件路径作为第一个参数。此外,AddPageRoute将路由模板作为第二参数,而不是路由定义,其中任何约束被单独定义。

最后一个例子说明将所有请求映射到单个文件。如果站点内容存储在特定位置(数据库,Markdown文件),并且由单个文件(例如 “index.cshtml” )负责根据 URL 定位内容,然后将其处理为HTML,则可以执行此操作:


 public void ConfigureServices(IServiceCollection services)
 {
  services
   .AddMvc()
   .AddRazorPagesOptions(options => {
     options.Conventions.AddPageRoute("/index", "{*url}");
  });
 }

路由模板(*)通配符表示“全部”。即使使用此配置,磁盘上的现有文件和URL之间的匹配规则仍然正常运行。

总结

Razor 页面中的路由系统非常直观,基于文件位置,但如果需要覆盖默认约定,它也非常强大,可配置。

原文:《Routing in Razor Pages》https://www.mikesdotnetting.com/article/310/routing-in-razor-pages

The above is the detailed content of A detailed introduction to ASP.NET Core Razor page routing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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