9

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.

2 Answers 2

12

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

Sign up to request clarification or add additional context in comments.

2 Comments

IApplicationEnvironment is now in Microsoft.Extensions.PlatformAbstractions namespace
IApplicationEnvironment is now IHostingEnvironment in ASP.NET Core 1.0. More info here - docs.asp.net/en/latest/migration/rc1-to-rtm.html
6

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.