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

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:

  1. 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.
  2. The second example demands a combination of host header and port, in this case, 80.
  3. 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.

主站蜘蛛池模板: 扎赉特旗| 大余县| 郧西县| 固安县| 涿鹿县| 珠海市| 右玉县| 乳山市| 德格县| 包头市| 乐平市| 启东市| 鄂托克前旗| 山东省| 乐清市| 浏阳市| 襄垣县| 福州市| 霍邱县| 岐山县| 河南省| 微博| 伊春市| 慈溪市| 赣榆县| 金山区| 义乌市| 阿鲁科尔沁旗| 商丘市| 灌阳县| 定安县| 依兰县| 东乡县| 万荣县| 广南县| 南开区| 台安县| 仙游县| 白玉县| 岫岩| 顺平县|