I have a self hosting Nancy instance and embedded the content in the assembly (a few sshtml views and a few css files)
This is the content of my DefaultNancyBootstrapper
public class CustomBootstrapper : DefaultNancyBootstrapper
{
private byte[] favicon;
protected override byte[] FavIcon
{
get { return this.favicon ?? (this.favicon = LoadFavIcon()); }
}
private byte[] LoadFavIcon()
{
using (var resourceStream = GetType().Assembly.GetManifestResourceStream("FrontEnd.Webinterface.Views.Content.Images.Icons.FavIcon.ico"))
{
var memoryStream = new MemoryStream();
resourceStream.CopyTo(memoryStream);
return memoryStream.GetBuffer();
}
}
protected override NancyInternalConfiguration InternalConfiguration
{
get
{
return NancyInternalConfiguration.WithOverrides(OnConfigurationBuilder);
}
}
void OnConfigurationBuilder(NancyInternalConfiguration x)
{
x.ViewLocationProvider = typeof(ResourceViewLocationProvider);
}
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string[] assemblyNames = assembly.GetManifestResourceNames();
base.ConfigureApplicationContainer(container);
ResourceViewLocationProvider.RootNamespaces.Add(
Assembly.GetAssembly(typeof(MainModule)), "FrontEnd.Webinterface.Views");
}
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
CookieBasedSessions.Enable(pipelines);
}
protected override IEnumerable<ModuleRegistration> Modules
{
get
{
return
AppDomainAssemblyTypeScanner
.TypesOf<INancyModule>(ScanMode.All)
.NotOfType<DiagnosticModule>()
.Select(t => new ModuleRegistration(t))
.ToArray();
}
}
}
I'm completely stuck how to embed my static CSS files - Can anyone help me out?
EDIT:
Solution is quite simple if this is in your Nancy Assembly https://github.com/NancyFx/Nancy/blob/master/src/Nancy.Embedded/Conventions/EmbeddedStaticContentConventionBuilder.cs
protected override void ConfigureConventions(NancyConventions nancyConventions)
{
Assembly Assembly = System.Reflection.Assembly.GetExecutingAssembly();
nancyConventions.StaticContentsConventions.Add(EmbeddedStaticContentConventionBuilder.AddDirectory("Static", Assembly, @"Webinterface/Static"));
nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("Views", @"Webinterface/Views"));
}
Although you've already answered your question yourself I'd like to add a couple of lines regarding the solution that worked fine for me.
First of all here are some links with the discussion on the topic containing explanations how things work:
Second, instead of EmbeddedStaticContentConventionBuilder
that you mentioned in your question I used implementation based on the source code from the second link by Patrick Steele, which is much simpler:
public static class StaticResourceConventionBuilder
{
public static Func<NancyContext, string, Response> AddDirectory(string requestedPath, Assembly assembly, string namespacePrefix)
{
return (context, s) =>
{
var path = context.Request.Path;
if (!path.StartsWith(requestedPath))
{
return null;
}
string resourcePath;
string name;
var adjustedPath = path.Substring(requestedPath.Length + 1);
if (adjustedPath.IndexOf('/') >= 0)
{
name = Path.GetFileName(adjustedPath);
resourcePath = namespacePrefix + "." + adjustedPath
.Substring(0, adjustedPath.Length - name.Length - 1)
.Replace('/', '.');
}
else
{
name = adjustedPath;
resourcePath = namespacePrefix;
}
return new EmbeddedFileResponse(assembly, resourcePath, name);
};
}
}
And then
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureConventions(NancyConventions nancyConventions)
{
base.ConfigureConventions(nancyConventions);
nancyConventions.StaticContentsConventions
.Add(StaticResourceConventionBuilder.AddDirectory("/Scripts", GetType().Assembly, "MyAssembly.Scripts"));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.