I am trying to build a minimal viable web site as a .NET Core project using Nancy with some backend processing and static files as frontend which resides in default project folder wwwroot
. The main problem is I don't understand how to make the app respond with static files, because default conventions don't apply to the new .NET Core project system.
Building Nancy applications as classic .NET Framework applications is well documented and there are many samples on the web on how to do it. But .NET Core projects (.xproj
) differ a lot from classic .NET Framework projects (.csproj
). I like the new project system a lot, but I don't understand how to integrate Nancy parts into it. And there is a lack of documentation and samples on how to do it.
Serve static files Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method. For more information, see Content root and Web root. The preceding code was created with the web app template.
ASP.NET Core can serve static files—HTML files, images, JavaScript files, etc. —directly to clients. To enable ASP.NET Core to serve static files, you must use the framework's Static Files Middleware in your application, and you must specify the necessary configuration.
We must include Microsoft. AspNetCore. StaticFiles middleware in the request pipeline.
Static files like JavaScript files, images, CSS files that we have on the file system are the assets that ASP.NET Core application can serve directly to clients. Static files are typically located in the web root (wwwroot) folder.
ASP.NET Core - Serving Static Files 1 Install StaticFiles Middleware. The Microsoft.AspNetCore.StaticFiles middleware package is already included in the meta package Microsoft.AspNetCore.All, so we don't need to install it separately in ASP.NET Core 2.x application. 2 Using StaticFiles Middleware. ... 3 Set Default File. ...
In ASP.NET Core, the web root directory holds the static files. By default, it is the {content root}/wwwroot directory, but you can change it using the UseWebRoot () method. In the Program class, the CreateDefaultBuilder () method initializes the content root. You can have different directories for each type of file that your application serves.
How should your migration from ASP.NET MVC to ASP.NET Core handle serving static files? ASP.NET MVC apps, hosted by IIS, typically host static files directly from the app. ASP.NET MVC supports placing static files side by side with files that should be kept private on the server.
Thank you. Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET Core app serves directly to clients by default. Static files are stored within the project's web root directory.
TL;DR: GitHub repository, where the demo projects with all needed plumbing code resides. For Nancy v. 1.4.3 as well as for prerelease v. 2.0.0-barneyrubble.
Nancy v. 2, which supports .NET Core and .NET Standard is still in prerelease state, so even if you would like to stick with stable v. 1 branch - no problem.
Here's a step-by-step on how to do it from the scratch, which worked for me:
1) Create a new Empty ASP.NET Core Web Application
2) (Mandatory if you would like to stick with Nancy v. 1) Open project.json
, remove "Microsoft.NETCore.App"
dependency and change target framework from "netcoreapp1.0"
to "net46"
, so frameworks
section would look like that:
"frameworks": {
"net46": {}
},
3) Add the following dependencies to project.json: "Microsoft.AspNetCore.Owin"
and "Nancy"
. At the time of writing the dependencies section of project.json
for v. 1:
"dependencies": {
// Ommited dependencies
"Microsoft.AspNetCore.Owin": "1.0.0",
"Nancy": "1.4.3"
},
For v. 2:
"dependencies": {
// Ommited dependencies
"Microsoft.AspNetCore.Owin": "1.0.0",
"Nancy": "2.0.0-barneyrubble"
},
4) Create a class implementing IRootPathProvider
and will provide a path to your wwwroot
folder (WebRootPath
property) at runtime by utilizing IHostingEnvironment
object:
public class AppRootPathProvider : IRootPathProvider
{
private readonly IHostingEnvironment _environment;
public AppRootPathProvider(IHostingEnvironment environment)
{
_environment = environment;
}
public string GetRootPath()
{
return _environment.WebRootPath;
}
}
5) Create a class derived from DefaultNancyBootstrapper
, which will retrieve IHostingEnvironment
object and pass it to the previously defined Root Provider. It will also change default StaticContentsConventions
with your own:
public class AppNancyBootstrapper : DefaultNancyBootstrapper
{
public AppNancyBootstrapper(IHostingEnvironment environment)
{
RootPathProvider = new AppRootPathProvider(environment);
}
protected override void ConfigureConventions(NancyConventions conventions)
{
base.ConfigureConventions(conventions);
conventions.StaticContentsConventions.AddDirectory("css");
conventions.StaticContentsConventions.AddDirectory("js");
conventions.StaticContentsConventions.AddDirectory("fonts");
}
protected override IRootPathProvider RootPathProvider { get; }
}
6) Open Startup
class and replace the last line in Configure
method with this one:
app.UseOwin(x => x.UseNancy(options => options.Bootstrapper = new AppNancyBootstrapper(env)));
It leverages Bootstrapper created in the previous step.
7) Now it's time to define your NancyModule
. V. 1:
public class AppNancyModule : NancyModule
{
public AppNancyModule()
{
Get["/"] = _ => View["index"];
Get["/{fileName}"] = parameters => View[parameters.fileName];
}
}
V. 2:
public class AppNancyModule : NancyModule
{
public AppNancyModule()
{
Get("/", _ => View["index"]);
Get("/{fileName}", parameters => View[parameters.fileName]);
}
}
And you are good to go! Just place your static files in wwwroot
- and fire off.
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