- 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.
- Java持續(xù)交付
- WordPress Plugin Development Cookbook(Second Edition)
- 琢石成器:Windows環(huán)境下32位匯編語言程序設(shè)計(jì)
- Scala編程實(shí)戰(zhàn)(原書第2版)
- Hands-On Natural Language Processing with Python
- Oracle 18c 必須掌握的新特性:管理與實(shí)戰(zhàn)
- ExtJS高級(jí)程序設(shè)計(jì)
- Arduino家居安全系統(tǒng)構(gòu)建實(shí)戰(zhàn)
- Access 2010數(shù)據(jù)庫應(yīng)用技術(shù)(第2版)
- Rust游戲開發(fā)實(shí)戰(zhàn)
- Getting Started with Electronic Projects
- 零基礎(chǔ)學(xué)Java第2版
- Java程序設(shè)計(jì)教程
- 面向?qū)ο蟪绦蛟O(shè)計(jì)及C++實(shí)驗(yàn)指導(dǎo)(第3版)
- Access 2013數(shù)據(jù)庫應(yīng)用案例課堂