【问题标题】:ASP.NET Core Error Development Mode only in AreaASP.NET Core 错误开发模式仅在区域
【发布时间】:2019-03-25 00:32:17
【问题描述】:

在 Azure 中,我部署了一个小应用程序:https://jsnamespace.azurewebsites.net/。 在 localhost 中,它在导航到 localhost/Admin 时运行良好。 但是,在部署的应用程序中,导航到 https://jsnamespace.azurewebsites.net/Admin 会产生错误:


错误。 处理您的请求时发生错误。 请求 ID:0HLLGMN77UU3U:00000001

开发模式 切换到开发环境将显示有关所发生错误的更详细信息。

不应在已部署的应用程序中启用开发环境,因为它可能导致向最终用户显示异常的敏感信息。对于本地调试,可以通过将 ASPNETCORE_ENVIRONMENT 环境变量设置为 Development 并重新启动应用程序来启用开发环境。


源代码可在 GitHub 上公开获取: https://github.com/cdaley78/JsNamespace

我已经看到其他关于在 Azure 中设置 ASPNETCORE_ENVIRONMENT 等问题的建议。考虑到除了位于区域中的代码外,一切正常,我觉得这不是必需的。

我错过了什么?

我已尝试在 Azure 中将 Web 应用程序中的 ASPNETCORE_ENVIRONMENT 设置为“开发”->“应用程序设置”。这没什么区别。

【问题讨论】:

    标签: c# asp.net azure github asp.net-core


    【解决方案1】:
    1. 如果你设置ASPNETCORE_ENVIRONMENT=Development(然后重启应用服务),你会发现它抱怨没有找到相关的View文件:李>

    发生此错误是因为您在 the wrong way 中设置了 Admin/Home/Index.cshtml

        <Compile Remove="Areas\**" />
        <Content Remove="Areas\**" />
        <EmbeddedResource Remove="Areas\**" />
        <None Remove="Areas\**" />
    
        ...
    
        <Compile Include="Areas\Admin\Controllers\HomeController.cs" />
    
        ...
    
        <None Include="Areas\Admin\Views\Home\Index.cshtml" />
    
    1. 要解决该问题,请删除 Areas/Admin 周围的那些不必要的配置,如下所示:
    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>net461</TargetFramework>
        <DebugType>full</DebugType>
      </PropertyGroup>
    
      <ItemGroup>
        <!-- <Compile Remove="Areas\**" />
        <Content Remove="Areas\**" />
        <EmbeddedResource Remove="Areas\**" />
        <None Remove="Areas\**" /> -->
      </ItemGroup>
    
      <ItemGroup>
        <Content Remove="bundleconfig.json" />
        <Content Remove="compilerconfig.json" />
      </ItemGroup>
    
      <ItemGroup>
        <_ContentIncludedByDefault Remove="compilerconfig.json" />
      </ItemGroup>
    
      <ItemGroup>
        <!-- <Compile Include="Areas\Admin\Controllers\HomeController.cs" /> -->
      </ItemGroup>
    
      <ItemGroup>
        <!-- <None Include="Areas\Admin\Views\Home\Index.cshtml" /> -->
        <None Include="bundleconfig.json" />
        <None Include="compilerconfig.json" />
      </ItemGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore" Version="2.1.7" />
        <PackageReference Include="Microsoft.AspNetCore.CookiePolicy" Version="2.1.2" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />
        <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.1.0" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0" />
      </ItemGroup>
    
      <ItemGroup>
        <Folder Include="Areas\Admin\Data\" />
        <Folder Include="Areas\Admin\Models\" />
      </ItemGroup>
    
    </Project>
    
    1. 现在对我来说效果很好:

    【讨论】:

    • 谢谢你。我不确定项目文件中的噪音是如何引入的。太糟糕了,该项目通过部署来暴露问题。我想知道我怎么能在 localhost 中看到这个。再次感谢。
    • @cdaley78 嗨,当我运行 Debug 按钮时,我还没有弄清楚 VisualStudio 本身拼写了什么魔法。我经常使用VSCode 来开发一个.NET Core App。但作为一种解决方法,如果我手动使用本地 IIS Express 运行它,它也会抛出。
    【解决方案2】:

    您似乎没有使用 AddEnvironmentVariables。在您的startup.cs 中,您应该这样做

    var config = new ConfigurationBuilder()
        .AddEnvironmentVariables()
        .Build();
    

    【讨论】:

    • 非常感谢您的帮助。事实证明我的项目文件中有噪音。我不确定它是如何到达那里的。但是,删除垃圾解决了这个问题。谢谢你的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2019-07-27
    • 1970-01-01
    相关资源
    最近更新 更多