-
Notifications
You must be signed in to change notification settings - Fork 1.5k
The root path
All paths that are used in Nancy are relative to something that is known as the root path. This is the path that tells Nancy where its resources are stored on the file system. The root path is provided to Nancy through the IRootPathProvider
interface, which defines a single method, GetRootPath
.
The various hosting options are all shipped with their own implementation of this interface, because the process of figuring out where the application is located on the file system varies from host to host.
Nancy will automatically use the first implementation, of the IRootPathProvider
, that it comes across so if you provide multiple implementations the end result will be non-deterministic. Care should be taken to ensure that only one implementation exists in the application domain of the running Nancy application.
Providing a custom root path is a two part process. First you need to create your own implementation of the IRootPathProvider
interface.
public class CustomRootPathProvider : IRootPathProvider
{
public string GetRootPath()
{
return "What ever path you want to use as your application root";
}
}
Note: The root path should be an absolute path. While relative paths work when locating views, they will not work for serving static content because they are considered unsafe.
Once that is done, the next thing you need to do is to let Nancy know that it should be using your own implementation, instead of going of and try to locate one for you (remember there are implementations in each host, so if you provide a second one, the result will be non-deterministic).
To let Nancy know it should use your implementation, you simply create a new bootstrapper (by inheriting from the bootstrapper you are using) and override the RootPathProvider property and return a new instance of your own root path provider.
public class CustomBootstrapper : DefaultNancyBootstrapper
{
protected override IRootPathProvider RootPathProvider
{
get { return new CustomRootPathProvider(); }
}
}
When your application runs, the bootstrapper will register this implementation in the current container and will be used when dependencies on the IRootPathProvider
are resolved.
To upload a file in Nancy you need to take the content stream of the uploaded file, create a file on disk and write that stream to disk.
var uploadDirectory = Path.Combine(pathProvider.GetRootPath(), "Content", "uploads");
if (!Directory.Exists(uploadDirectory))
{
Directory.CreateDirectory(uploadDirectory);
}
foreach (var file in Request.Files)
{
var filename = Path.Combine(uploadDirectory, file.Name);
using (FileStream fileStream = new FileStream(filename, FileMode.Create))
{
file.Value.CopyTo(fileStream);
}
}
However, you may be wondering what is pathProvider
. This variable is passed into our module constructor which gives us access to the root path of our application by calling GetRootPath()
and then you can save to a folder within your application.
public HomeModule(IRootPathProvider pathProvider)
NOTE : For Mono users less than version 4, there is a StaticConfiguration. AllowFileStreamUploadAsync
property that should be set to false if you are uploading files over 80mb
« Part 13. Testing your application — Documentation overview — Part 15. Managing static content »
- Introduction
- Exploring the Nancy module
- Routing
- Taking a look at the DynamicDictionary
- Async
- View Engines
- Using Models
- Managing static content
- Authentication
- Lifecycle of a Nancy Application
- Bootstrapper
- Adding a custom FavIcon
- Diagnostics
- Generating a custom error page
- Localization
- SSL Behind Proxy
- Testing your application
- The cryptography helpers
- Validation
- Hosting Nancy with ASP.NET
- Hosting Nancy with WCF
- Hosting Nancy with Azure
- Hosting Nancy with Suave.IO
- Hosting Nancy with OWIN
- Hosting Nancy with Umbraco
- Hosting Nancy with Nginx on Ubuntu
- Hosting Nancy with FastCgi
- Self Hosting Nancy
- Implementing a Host
- Accessing the client certificate when using SSL
- Running Nancy on your Raspberry Pi
- Running Nancy with ASP.NET Core 3.1