Warm tip: This article is reproduced from stackoverflow.com, please click
asp.net-core asp.net-core-2.0 asp.net-core-mvc

Get application virtual base path in aspnet core

发布于 2020-04-16 11:59:24

I know that is located in HttpContext.Request.PathBase, however I need it to configure my cookie before I have any HttpContext (in Startup.cs).

My problem:

When devops configure application they have to set path twice. Once in IIS application (so hosting knows what should be served) and in my appsettings.json (so application knows where to set cookies - multiple instances can work on server). I would like to configure it once in IIS, and have config passed to my application.

Questioner
Shadow
Viewed
135
Tratcher 2018-02-27 11:32

You can check the environment variable ASPNETCORE_APPL_PATH. This is the variable AspNetCoreModule provides so that PathBase can be set correctly. See https://github.com/aspnet/IISIntegration/blob/df88e322cc5e52db3dbce4060d5bc7db88edb8e4/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs#L19