我有一个自我托管Nancy实例并在程序集中嵌入了内容(一些sshtml视图和一些css文件)
这是我的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();
}
}
}
我完全不知道如何嵌入我的静态CSS文件 - 任何人都可以帮助我吗?
编辑:
中的解决方案非常简单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"));
}
答案 0 :(得分:0)
虽然您已经自己回答了自己的问题,但我想补充一些与我合作的解决方案。
首先,这里有一些关于该主题讨论的链接,其中包含有关工作原理的解释:
其次,不是你在问题中提到的EmbeddedStaticContentConventionBuilder
,而是基于Patrick Steele的第二个链接的源代码实现了实现,这更简单:
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);
};
}
}
然后
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureConventions(NancyConventions nancyConventions)
{
base.ConfigureConventions(nancyConventions);
nancyConventions.StaticContentsConventions
.Add(StaticResourceConventionBuilder.AddDirectory("/Scripts", GetType().Assembly, "MyAssembly.Scripts"));
}
}