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

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.

主站蜘蛛池模板: 沈阳市| 仙桃市| 嘉义县| 乐昌市| 乌鲁木齐县| 田阳县| 绥德县| 紫阳县| 江川县| 肥城市| 汝南县| 榆林市| 西宁市| 云霄县| 隆安县| 荆门市| 明水县| 梁河县| 汉寿县| 兖州市| 肇庆市| 兴和县| 新兴县| 久治县| 盱眙县| 綦江县| 页游| 宜黄县| 竹北市| 五华县| 乌兰县| 浦江县| 侯马市| 武山县| 邢台市| 凤冈县| 凌云县| 济宁市| 元谋县| 松潘县| 庆元县|