[英]Received a DirectoryNotFoundException: c:\windows\system32\inetsrv\Static_Files When application was moved to a live server
I have an application that utilizes a directory of static files outside of wwwroot.我有一个应用程序,它使用 wwwroot 之外的静态文件目录。 Thus, I created a new middleware for this folder in the Startup.cs file.因此,我在 Startup.cs 文件中为此文件夹创建了一个新的中间件。 It works locally however, when put into production the folder can't be found even though it is in the project.然而,它在本地工作,当投入生产时,即使它在项目中也找不到文件夹。
I have added error checks to my Program.cs file.我在 Program.cs 文件中添加了错误检查。 It seems to be coming from the new StaticOptions middleware I created.它似乎来自我创建的新的 StaticOptions 中间件。 However, I think this might be an issue with asp.net core 2.2但是,我认为这可能是 asp.net core 2.2 的问题
Startup.cs启动文件
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Static_Files")),
RequestPath = "/StaticFiles"
});
Error错误
DirectoryNotFoundException: c:\windows\system32\inetsrv\Static_Files\
The directory can't be found even though it is there.即使目录在那里也找不到。
This is a bug in ASP.NET Core 2.2 which has been reported in GitHub and Microsoft ASP.NET Core team has provided a solution as follows.这是 ASP.NET Core 2.2 中的一个 bug,已在GitHub 上报告,Microsoft ASP.NET Core 团队提供了如下解决方案。
Add the following class somewhere in your application.在应用程序的某处添加以下类。 This code comes here .此代码来自此处。
internal class CurrentDirectoryHelpers
{
internal const string AspNetCoreModuleDll = "aspnetcorev2_inprocess.dll";
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[System.Runtime.InteropServices.DllImport(AspNetCoreModuleDll)]
private static extern int http_get_application_properties(ref IISConfigurationData iiConfigData);
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
private struct IISConfigurationData
{
public IntPtr pNativeApplication;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzFullApplicationPath;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzVirtualApplicationPath;
public bool fWindowsAuthEnabled;
public bool fBasicAuthEnabled;
public bool fAnonymousAuthEnable;
}
public static void SetCurrentDirectory()
{
try
{
// Check if physical path was provided by ANCM
var sitePhysicalPath = Environment.GetEnvironmentVariable("ASPNETCORE_IIS_PHYSICAL_PATH");
if (string.IsNullOrEmpty(sitePhysicalPath))
{
// Skip if not running ANCM InProcess
if (GetModuleHandle(AspNetCoreModuleDll) == IntPtr.Zero)
{
return;
}
IISConfigurationData configurationData = default(IISConfigurationData);
if (http_get_application_properties(ref configurationData) != 0)
{
return;
}
sitePhysicalPath = configurationData.pwzFullApplicationPath;
}
Environment.CurrentDirectory = sitePhysicalPath;
}
catch
{
// ignore
}
}
}
Finally, in the Main function of the Program class add the following line as the first thing in the function.最后,在Program类的Main函数中添加以下行作为函数中的第一行。
CurrentDirectoryHelpers.SetCurrentDirectory();
Relevant reference links :相关参考链接:
https://stackoverflow.com/a/54509770/10201850 https://stackoverflow.com/a/54509770/10201850
https://github.com/aspnet/AspNetCore.Docs/issues/9865 https://github.com/aspnet/AspNetCore.Docs/issues/9865
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.