简体   繁体   English

收到 DirectoryNotFoundException: c:\\windows\\system32\\inetsrv\\Static_Files 当应用程序移动到实时服务器时

[英]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.

相关问题 访问路径 'c:\windows\system32\inetsrv\config\' 被拒绝 - Access to the path 'c:\windows\system32\inetsrv\config\' is denied 找不到文件 'c:\\windows\\system32\\inetsrv\\xxx.xlsx' - Could not find file 'c:\windows\system32\inetsrv\xxx.xlsx' WebClient下载-“拒绝访问路径'c:\\\\ windows \\\\ system32 \\\\ inetsrv \\\\ MyPrintManager.exe'” - WebClient Download - “Access to the path 'c:\\windows\\system32\\inetsrv\\MyPrintManager.exe' is denied” File.Move错误-System.IO.FileNotFoundException:找不到文件'c:\\ windows \\ system32 \\ inetsrv - File.Move Error - System.IO.FileNotFoundException: Could not find file 'c:\windows\system32\inetsrv ASP零:System.IO.FileNotFoundException:找不到文件'c:\\ windows \\ system32 \\ inetsrv \\ log4net.config' - ASP Zero :System.IO.FileNotFoundException: Could not find file 'c:\windows\system32\inetsrv\log4net.config' 如何使用c#asp.net获取C:\\ Windows \\ System32 \\ inetsrv \\ config文件夹的访问控制 - How to get access control of C:\Windows\System32\inetsrv\config folder using c# asp.net 无法从C:\\ Windows \\ System32 \\ inetsrv \\ hwebcore.dll引用hwebcore.dll到visualstudio2013 - Can't able to refer the hwebcore.dll from C:\Windows\System32\inetsrv\hwebcore.dll to visualstudio2013 Windows Server Task Scheduler需要文件在System32文件夹中吗? - Windows Server Task Scheduler needs files to be in System32 folder? 当程序集位于“C:\Windows\System32”内时,我得到“无法找到运行此应用程序的运行时版本” - When assembly is inside 'C:\Windows\System32' i get 'Unable to find a version of the runtime to run this application' .Net MVC 4 项目失败并显示事件日志错误“模块 DLL C:\\WINDOWS\\system32\\inetsrv\\aspnetcore.dll 无法加载。 数据就是错误。” - .Net MVC 4 Project fails with Event Log Error “The Module DLL C:\WINDOWS\system32\inetsrv\aspnetcore.dll failed to load. The data is the error.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM