使用 OWIN 静态文件定义主页
Defining main page using OWIN static files
在 OWIN 的帮助下,我有一个自托管应用程序。另外,我需要提供一些静态文件。它有效,但最近我遇到了一个问题——默认文件停止工作。我的意思是早些时候如果你去本地主机它会重定向到 "main" 页面。但是现在主页只能通过直接路径访问,例如https://localhost:4443/index.html
这是我的启动方法:
public class Startup
{
// This code configures Web API. The Startup class is specified as a type
// parameter in the WebApp.Start method.
public void Configuration(IAppBuilder appBuilder)
{
try
{
appBuilder.UseOAuthAuthorizationServer(new OAuthOptions());
appBuilder.UseJwtBearerAuthentication(new JwtOptions());
var config = new MyHttpsSelfHostConfiguration("https://localhost:4443");
var start = DateTime.Now;
config.MapHttpAttributeRoutes();
var jsonFormatter = new JsonMediaTypeFormatter();
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
config.Routes.MapHttpRoute(
name: "WebLabApp",
routeTemplate: "api/{controller}/{action}/{id}"
);
ConfigureUnity(config);
appBuilder.UseWebApi(config);
Console.WriteLine($"Configure Unity in {DateTime.Now - start}");
start = DateTime.Now;
BusinessServicesHostLibrary.ContainerRegistry.RegisterAllBusinessServicesCommonClassesAndConnectionFactory();
Console.WriteLine($"Register all in {DateTime.Now - start}");
#if DEBUG
var contentDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent?.FullName;
#else
var contentDir = @".";
#endif
var fileSystem = new PhysicalFileSystem(contentDir);
var options = new FileServerOptions
{
EnableDefaultFiles = true,
DefaultFilesOptions = { DefaultFileNames = { "index.html" } },
FileSystem = fileSystem,
};
options.StaticFileOptions.FileSystem = fileSystem;
options.StaticFileOptions.ServeUnknownFileTypes = true;
appBuilder.UseFileServer(options);
config.EnsureInitialized();
}
catch (Exception ex)
{
if (ex is System.Reflection.ReflectionTypeLoadException)
{
var typeLoadException = ex as ReflectionTypeLoadException;
var loaderExceptions = typeLoadException.LoaderExceptions;
foreach (var x in loaderExceptions) Console.WriteLine(x);
}
throw;
}
}
我做错了什么?
解决方法很简单——我们需要调用 appBuilder.UseWebApi(config);
在配置方法的末尾
在 OWIN 的帮助下,我有一个自托管应用程序。另外,我需要提供一些静态文件。它有效,但最近我遇到了一个问题——默认文件停止工作。我的意思是早些时候如果你去本地主机它会重定向到 "main" 页面。但是现在主页只能通过直接路径访问,例如https://localhost:4443/index.html
这是我的启动方法:
public class Startup
{
// This code configures Web API. The Startup class is specified as a type
// parameter in the WebApp.Start method.
public void Configuration(IAppBuilder appBuilder)
{
try
{
appBuilder.UseOAuthAuthorizationServer(new OAuthOptions());
appBuilder.UseJwtBearerAuthentication(new JwtOptions());
var config = new MyHttpsSelfHostConfiguration("https://localhost:4443");
var start = DateTime.Now;
config.MapHttpAttributeRoutes();
var jsonFormatter = new JsonMediaTypeFormatter();
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
config.Routes.MapHttpRoute(
name: "WebLabApp",
routeTemplate: "api/{controller}/{action}/{id}"
);
ConfigureUnity(config);
appBuilder.UseWebApi(config);
Console.WriteLine($"Configure Unity in {DateTime.Now - start}");
start = DateTime.Now;
BusinessServicesHostLibrary.ContainerRegistry.RegisterAllBusinessServicesCommonClassesAndConnectionFactory();
Console.WriteLine($"Register all in {DateTime.Now - start}");
#if DEBUG
var contentDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent?.FullName;
#else
var contentDir = @".";
#endif
var fileSystem = new PhysicalFileSystem(contentDir);
var options = new FileServerOptions
{
EnableDefaultFiles = true,
DefaultFilesOptions = { DefaultFileNames = { "index.html" } },
FileSystem = fileSystem,
};
options.StaticFileOptions.FileSystem = fileSystem;
options.StaticFileOptions.ServeUnknownFileTypes = true;
appBuilder.UseFileServer(options);
config.EnsureInitialized();
}
catch (Exception ex)
{
if (ex is System.Reflection.ReflectionTypeLoadException)
{
var typeLoadException = ex as ReflectionTypeLoadException;
var loaderExceptions = typeLoadException.LoaderExceptions;
foreach (var x in loaderExceptions) Console.WriteLine(x);
}
throw;
}
}
我做错了什么?
解决方法很简单——我们需要调用 appBuilder.UseWebApi(config);
在配置方法的末尾