I can't seem to find an equivalent in Nancy for System.Web.HttpContext.Current.Server.MapPath() in the Nancy framework.
I just want to load a textfile relative to the application service.
I see this in the assembly
using Nancy;
using System;
namespace Nancy.Hosting.Self
{
public class FileSystemRootPathProvider : IRootPathProvider, IHideObjectMembers
{
public FileSystemRootPathProvider();
public string GetRootPath();
}
}
I'm not sure how to use.
update: I just figured out anything I need to load can just be read/written from the bin/relase/ directory. Is that the assumed way to do it in a Nancy Self Hosting environment? I guess that would make sense.
As you most likely already know, the Server.MapPath method is a typical approach that we could use since classic ASP and ASP.NET Web Forms and MVC versions up to and including 5. Here's a typical way to use it:
The MapPath method is used to define a relative virtual path for a physical directory on the server. Note: This method cannot be used in Session.OnEnd and Application.OnEnd. path: It stores a string value that defines the relative or virtual path to map to a physical directory.
NOTE: The original .NET Framework Server.MapPath worked slightly differently returning d:\website\file.txt when called from with a path = "\file.txt", this returns just "file.txt". Just remove the initial "", to get similar results. Hope that helps Where does the env variable come from?
@RolandWales, if you're using ASP.NET Core 6 with the new hosting model (without Startup.cs file), your env variable equivalent (originally a parameter of the Configure method in the Startup.cs class) is the builder.Environment property (in the Program.cs file).
You can take a dependency on IRootPathProvider
and use that to call GetRootPath()
that will give you the root of your application and you can add from there (I would recommend using Path.Combine
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With