|
@@ -34,6 +34,7 @@ using XdCxRhDw.Dto;
|
|
|
using Autofac.Core;
|
|
|
using Microsoft.Owin.FileSystems;
|
|
|
using Microsoft.Owin.StaticFiles;
|
|
|
+using System.Threading.Tasks;
|
|
|
|
|
|
[assembly: OwinStartup(typeof(XdCxRhDW.WebApi.Startup))]
|
|
|
|
|
@@ -50,21 +51,18 @@ namespace XdCxRhDW.WebApi
|
|
|
/// <param name="app"></param>
|
|
|
public void Configuration(IAppBuilder app)
|
|
|
{
|
|
|
- app.UseStaticFiles("/wwwroot");
|
|
|
- Directory.CreateDirectory("wwwroot");
|
|
|
- //启用静态目录浏览
|
|
|
- //string root = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
- //var physicalFileSystem = new PhysicalFileSystem(Path.Combine(root, "wwwroot"));
|
|
|
- //var options = new FileServerOptions
|
|
|
- //{
|
|
|
- // RequestPath = PathString.Empty,
|
|
|
- // EnableDefaultFiles = true,
|
|
|
- // FileSystem = physicalFileSystem
|
|
|
- //};
|
|
|
- //options.StaticFileOptions.FileSystem = physicalFileSystem;
|
|
|
- //options.StaticFileOptions.ServeUnknownFileTypes = false;
|
|
|
- //app.UseFileServer(options);
|
|
|
-
|
|
|
+ //启用目录浏览和静态文件
|
|
|
+ var physicalFileSystem = new PhysicalFileSystem(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot"));//目录浏览物理地址
|
|
|
+ var options = new FileServerOptions
|
|
|
+ {
|
|
|
+ RequestPath = new PathString("/wwwroot"),//目录浏览地址
|
|
|
+ EnableDefaultFiles = true,
|
|
|
+ EnableDirectoryBrowsing = true,//启用目录浏览
|
|
|
+ FileSystem = physicalFileSystem
|
|
|
+ };
|
|
|
+ options.StaticFileOptions.FileSystem = physicalFileSystem;
|
|
|
+ options.StaticFileOptions.ServeUnknownFileTypes = true;//允许下载wwwroot中的所有类型文件
|
|
|
+ app.UseFileServer(options);
|
|
|
|
|
|
HttpConfiguration config = new HttpConfiguration();
|
|
|
IEnumerable<ModelValidatorProvider> modelValidatorProviders = config.Services.GetModelValidatorProviders();
|
|
@@ -115,7 +113,7 @@ namespace XdCxRhDW.WebApi
|
|
|
//单例模式注入Service
|
|
|
builder.RegisterTypes(serviceType).SingleInstance();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
var container = builder.Build();
|
|
|
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
|
|
|
|
|
@@ -191,15 +189,21 @@ namespace XdCxRhDW.WebApi
|
|
|
base.OnException(context);
|
|
|
Serilog.Log.Error(context.Exception, context.Exception.Message);
|
|
|
//LogFile.WriteError(context.Exception.Message);
|
|
|
+ string msg = context.Exception.Message;
|
|
|
+ if (context.Exception.GetType() == typeof(FileNotFoundException))
|
|
|
+ {
|
|
|
+ //防止程序路径泄露到前端
|
|
|
+ msg = "未能找到文件" + context.Exception.Message.Substring(context.Exception.Message.LastIndexOf("\\") + 1);
|
|
|
+ }
|
|
|
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.OK)
|
|
|
{
|
|
|
Content = new StringContent(
|
|
|
JsonConvert.SerializeObject(
|
|
|
- new
|
|
|
+ new AjaxResult
|
|
|
{
|
|
|
- code = -1,
|
|
|
- data = "",
|
|
|
- msg = context.Exception.Message
|
|
|
+ code = 0,
|
|
|
+ data = null,
|
|
|
+ msg = msg
|
|
|
}), Encoding.UTF8, "text/json")
|
|
|
});
|
|
|
}
|