- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 352字
- 2021-06-18 18:35:58
Matching route parameters
Remember that a template needs to have a controller token and an action token; these are the only required tokens and have special meaning. A controller will match a controller class and an action will match one of its public methods. Any other template parameter will match the parameter of the same name in the action method. For example, take a route with the following template:
{controller=Search}/{action=Query}/{phrase}
That route will map to this Querymethod in a class called SearchController:
public IActionResult Query(string phrase) { ... }
If a route token is optional, then it must map to a parameter that has a default value:
{controller=Account}/{action=List}/{page?}
A matching method would have the following signature:
public IActionResult List(int page = 0)
Notice that the page parameter is an int instance that has a default value of 0. This might be used, for example, for paging, where the default page is the first one (zero-based). This would be the same as having a token with a default of 0 and mapping it to a parameter without a default value.
So far, we've only seen how we can map simple values of strings or basic types; we will soon see how we can use other types.
We've mentioned that the action parameter is required, but, although this is true in a way, its value may be skipped. In this case, ASP.NET Core will use a value from the HTTP action header, such as GET, POST, PUT, DELETE, and so on. This is particularly useful in the case of web APIs and is often very intuitive. So, for example, take a route with a template such as this:
api/{controller}/{id}
Say it has a request of this:
GET /api/Values/12
It can be mapped to a method such as this, in a controller named ValuesController:
public IActionResult Get(int id) { ... }
So, we just learned how template parameters are matched from templates to controller classes' methods. Now we will learn about dynamic routing, where the mapping is not pre-defined.
- Learning Single:page Web Application Development
- Oracle WebLogic Server 12c:First Look
- 匯編語言程序設(shè)計(第2版)
- Redis Essentials
- 單片機C語言程序設(shè)計實訓(xùn)100例
- 大話Java:程序設(shè)計從入門到精通
- Image Processing with ImageJ
- SQL Server 2016 從入門到實戰(zhàn)(視頻教學(xué)版)
- 算法圖解
- ASP.NET開發(fā)寶典
- Yii2 By Example
- 深入理解Java虛擬機:JVM高級特性與最佳實踐
- Java 11 and 12:New Features
- HTML5程序開發(fā)范例寶典
- AngularJS Directives Cookbook