- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 350字
- 2021-06-18 18:35:58
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.
- 密碼學原理與Java實現(xiàn)
- 大學計算機基礎(chǔ)實驗教程
- 單片機C語言程序設(shè)計實訓100例:基于STC8051+Proteus仿真與實戰(zhàn)
- Software Testing using Visual Studio 2012
- Programming ArcGIS 10.1 with Python Cookbook
- ASP.NET Core 2 and Vue.js
- Python Network Programming Cookbook(Second Edition)
- Internet of Things with Intel Galileo
- Mastering RStudio:Develop,Communicate,and Collaborate with R
- Linux操作系統(tǒng)基礎(chǔ)案例教程
- 網(wǎng)站構(gòu)建技術(shù)
- Visual C++開發(fā)入行真功夫
- Scala Reactive Programming
- OpenCV with Python By Example
- Hands-On GUI Programming with C++ and Qt5