- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 177字
- 2021-06-18 18:35:58
Host selection from attributes
Starting in ASP.NET 3, it is also possible to restrict a route based on the host header and port. You can either do that through attributes or by using fluent (code-based) configuration.
Here's an example of using attributes:
[Host("localhost", "127.0.0.1")]
public IActionResult Local() { ... }
[Host("localhost:80")]
public IActionResult LocalPort80() { ... }
[Host(":8080")]
public IActionResult Port8080() { ... }
We have three examples of using the [Host] attribute here:
- The first one makes the Localaction method reachable only if the local header is localhost or 127.0.0.1; any number of host headers can be provided.
- The second example demands a combination of host header and port, in this case, 80.
- The final one just expects port 8080.
The [Host] attribute can, of course, be combined with any [Http*] or [Route] ones.
Here's how to do this through code:
endpoints.MapControllerRoute("Local", "Home/Local").RequireHost("localhost", "127.0.0.1");
This example only accepts requests from either "localhost" or "127.0.0.1" (generally these are synonyms) for the given route.
Now, the next topic will be how to specify defaults for route template parameters.
推薦閱讀
- C語(yǔ)言程序設(shè)計(jì)案例教程
- LaTeX Cookbook
- Java 開(kāi)發(fā)從入門(mén)到精通(第2版)
- JavaScript+DHTML語(yǔ)法與范例詳解詞典
- Interactive Data Visualization with Python
- 人臉識(shí)別原理及算法:動(dòng)態(tài)人臉識(shí)別系統(tǒng)研究
- Python數(shù)據(jù)分析(第2版)
- Full-Stack Vue.js 2 and Laravel 5
- Mastering JavaScript High Performance
- Python項(xiàng)目實(shí)戰(zhàn)從入門(mén)到精通
- Cocos2d-x Game Development Blueprints
- RESTful Web Clients:基于超媒體的可復(fù)用客戶(hù)端
- 硬件產(chǎn)品設(shè)計(jì)與開(kāi)發(fā):從原型到交付
- R的極客理想:量化投資篇
- 網(wǎng)頁(yè)設(shè)計(jì)與制作