官术网_书友最值得收藏!

Using route templates

A template is a relative URL, so it mustn't start with a slash (/). In it, you define the structure of your site, or, more accurately, the structure that you intend to make available. As ASP.NET Core is an MVC framework, the template should describe how to map the request to an action method in a controller. The following is the template:

{controller=Home}/{action=Index}/{id?}

It consists of sections separated by slashes, where each section has some tokens (inside curly braces).

Another example would be this:

sample/page

Here it is not clear what we want, as there are no mentions of controller or action. However, this is a perfectly valid template, and the required information needs to come from elsewhere.

A template can have the following elements:

  • Alphanumeric literals
  • String fragments inside curly braces ({}), which are named tokens and can be mapped to action method parameters
  • Named tokens with equal assignments (=) have default values, in case the token is not supplied in the URL; it doesn't make sense to have a token with a default value followed by a required token without
  • Tokens that end with a question mark (?), which are optional, meaning they are not required; optional tokens cannot be followed by required tokens
  • Tokens that start with a star (*), which are entirely optional and match anything; they need to be the last element in the template

Tokens are always alphanumeric character segments and can be separated by separator symbols (/, ?, -, (, ), and so on). However, you don't need to use separators; the following is perfectly valid—notice the lack of a slash between the action and id tokens:

{controller=Admin}/{action=Process}{id}

Another slightly more complex example follows, which involves adding a catch-all token querystring:

{controller=Admin}/{action=Process}/{?id}?{*querystring}

This template will match the following URLs:

Yet another perfectly valid example would be this:

api/{controller=Search}/{action=Query}?term={term}

That would match the following:

api?term=.net+core
api/Search?term=java
api/Search/Query?term=php

Note that any literals must be present exactly the same way as shown, in the URL, regardless of the casing.

Now, let's see how the route parameters specified in templates are matched.

主站蜘蛛池模板: 易门县| 鞍山市| 将乐县| 黄大仙区| 鸡东县| 大连市| 和平区| 习水县| 麦盖提县| 华池县| 犍为县| 城口县| 乌审旗| 抚松县| 石嘴山市| 大渡口区| 南投县| 丘北县| 平度市| 英吉沙县| 邵阳县| 通道| 永嘉县| 宁海县| 荔浦县| 调兵山市| 定日县| 丹东市| 五台县| 镇江市| 灌阳县| 乌苏市| 乌兰县| 阳原县| 临夏市| 通州区| 云阳县| 会昌县| 拉孜县| 忻城县| 竹山县|