linkerro
linkerro

Reputation: 5458

get application path in asp.net vnext

I've been trying to open a file in asp.net 5 and have not been having a lot of success.

In the past you used Server.MapPath or HostingEnvironment.ApplicationPhysicalPath. They are both gone in the OWIN based framework.

There is a HostingEnvironment class but it's in the System.Aspnet but it needs to be initialized by the hosting environment (it no longer has a static member for ApplicationPhysicalPath but I'm guessing the WebRoot member does that now. The problem is I can't find a reference to it anywhere.

I've also looked at Context.GetFeature<> but it doesn't seem to have any feature that would show the application path, just request and response related info. The code listing the features can be found here.

<snark>Is the ability to work with files a discontinued feature in ASP.NET?</snark>

There is also the possibility that I can't brain very well right now and missed something.

Upvotes: 9

Views: 7764

Answers (2)

Shaun Luttin
Shaun Luttin

Reputation: 141434

There are two approaches now:

using Microsoft.Extensions.PlatformAbstractions;

public Startup(IApplicationEnvironment appEnv)
{
    // approach 1
    var path01 = PlatformServices.Default.Application.ApplicationBasePath;

    // approach 2
    var path02 = appEnv.ApplicationBasePath;
}

Upvotes: 6

Kiran
Kiran

Reputation: 57939

You can get it from the ApplicationBasePath property of Microsoft.Framework.Runtime.IApplicationEnvironment serivce.

Example: https://github.com/aspnet/Mvc/blob/9f1cb655f6bb1fa0ce1c1e3782c43a2d45ca4e37/test/WebSites/FilesWebSite/Controllers/DownloadFilesController.cs#L28

Upvotes: 12

Related Questions