В ядре ASP.NET хост доступен через Request.Host.Host
. Если вы хотите разрешить переопределение хоста через параметр запроса, сначала проверьте Request.Query
.
Чтобы заставить параметр запроса узла распространяться на новые URL-адреса на основе маршрутов, добавьте этот код в app.UseMvc
конфигурация маршрута:
routes.Routes.Add(new HostPropagationRouter(routes.DefaultHandler));
И определите HostPropagationRouter
следующим образом:
/// <summary>
/// A router that propagates the request's "host" query parameter to the response.
/// </summary>
class HostPropagationRouter : IRouter
{
readonly IRouter router;
public HostPropagationRouter(IRouter router)
{
this.router = router;
}
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
if (context.HttpContext.Request.Query.TryGetValue("host", out var host))
context.Values["host"] = host;
return router.GetVirtualPath(context);
}
public Task RouteAsync(RouteContext context) => router.RouteAsync(context);
}