13

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.

Shadow
  • 2,089
  • 2
  • 23
  • 45

4 Answers4

8

There is a little confustion here that should be clarified.

You want to know application virtual path at application startup. However application virtual path is a concept of the hosting and specific request rather than underlying application. Hosting service uses this virtual path to map incoming url to application root. In IIS you can map multiple virtual paths to the same physical directory, e.g. /myApp1 and /myApp2 will point to the same application. Which of these paths do you want to get at application startup?

That's actually the reason why IHostingEnvironment interface does not provide any property for getting application virtual path. Another thing when application processes the request. In this case specific URL is requested and ASP.NET could provide requested virtual path in HttpContext.Request.PathBase.

You should reconsider your use case and check whether you actually need application virtual path for cookie configuration. It could be the XY problem. If you still have doubts regarding cookie configuration, please formulate it as a new question with specific details for your scenario.

CodeFuller
  • 30,317
  • 3
  • 63
  • 79
6

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

Tratcher
  • 5,929
  • 34
  • 44
0

I came across this question when looking for the answer myself and none of the current answers work for me (the use of environment variable ASPNETCORE_APPL_PATH looked promising but that also failed.)

My solution ugly but works:

    public static string? GetIISVirtualPath(this WebApplication app)
    {
        object iServer = app.Services.GetRequiredService<IServer>();
        PropertyInfo? pi = iServer.GetType().GetProperty("VirtualPath");
    
        return (pi?.GetValue(iServer) as string)?.ToLower();
    }

Rummaging about in the source the IServer implementation under IIS has an "VirtualPath" path property. So I grab that on startup.

Evil because MS can change this if they so desire, breaking this solution but I'll take that for now.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
-1

Just to add you can use @Url.Content("") in Javascript to get virtual directory base path.

<script src="@Url.Content("~/app/myapp.js")" type="text/javascript"></script>
Himalaya Garg
  • 1,525
  • 18
  • 23