我已经使用Owin自托管控制台应用程序设置了一个网站。我正在为静态文件服务,没有问题,网站静态部分的“root”工作正常,web API路由也工作正常。
如果我浏览到:
http://localhost/index.html它呈现了我所期望的一切。但我还没有弄清楚如何设置它,以便浏览:
http://localhost显示index.html (作为默认视图)。这只适用于IIS样式的站点。我如何让它与Owin self host一起工作?
发布于 2014-10-24 23:13:23
我这样做:
var physicalFileSystem = new PhysicalFileSystem(webPath);
var options = new FileServerOptions
{
EnableDefaultFiles = true,
FileSystem = physicalFileSystem
};
options.StaticFileOptions.FileSystem = physicalFileSystem;
options.StaticFileOptions.ServeUnknownFileTypes = true;
options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" };
appBuilder.UseFileServer(options);发布于 2015-01-16 02:37:26
fra答案的一个更详细的版本:
1- NuGet安装Microsoft.Owin.StaticFiles (我假设你已经通过NuGet安装了Microsoft.AspNet.WebApi.OwinSelfHost )
2-在您的解决方案中(在Visual Studio中)创建一个目录,并将所有客户端文件放入其中,例如
+Web
--+images
--+pages
------page1
------page2
--+scripts
--+css
---index.html注意:有一个根目录(web),其中包含所有其他目录,index.html直接位于根目录下。
3-现在,在包含您的web api路由配置的同一个类中,添加以下代码:
var physicalFileSystem = new PhysicalFileSystem(@".\Web"); //. = root, Web = your physical directory that contains all other static content, see prev step
var options = new FileServerOptions
{
EnableDefaultFiles = true,
FileSystem = physicalFileSystem
};
options.StaticFileOptions.FileSystem = physicalFileSystem;
options.StaticFileOptions.ServeUnknownFileTypes = true;
options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" }; //put whatever default pages you like here
appBuilder.UseFileServer(options);4-要使上一步代码正常工作:确保将Web目录(和所有嵌套目录)中所有文件的Copy to output directory属性设置为Copy Always选择文件|按F4,或者右键单击,然后单击属性|转到Copy to output directory
这就是全部内容:)
发布于 2015-09-07 04:39:31
也许这是一个迟来的答案,但是如果你只需要一个默认的文档,那么更少的代码就可以工作:
builder.UseDefaultFiles(new DefaultFilesOptions
{
DefaultFileNames = Enumerable.Repeat("index.html", 1).ToList()
});无论出于什么原因,都应该在builder.UseStaticFiles之前调用它。
Microsoft.Owin.StaticFiles的版本是3.0.1
https://stackoverflow.com/questions/25478222
复制相似问题