c# - 访问在 azure 服务器上运行的 .net core 应用程序中的共享 View 文件夹

标签 c# azure asp.net-core

在本地运行我的网站时,我访问共享 View 文件夹中的文件,如下所示:

string[] files = Directory.GetFiles("Views/Shared");

因为我通过 API 调用动态获取页面内容,并且某些字段具有写入的部分 View 来覆盖默认呈现。我只是获取与 api 调用中的某些内容匹配的共享 View 名称,将其设置在变量中,然后使用该变量在 .cshtml 页面中呈现部分 View 。

这在本地工作正常,但在部署到 Azure 后失败,但一些谷歌搜索告诉我,我应该访问项目文件的方法是使用 IWebHostEnvironment.ContentRootPath。同样,这在本地工作得很好;但部署到 Azure 后,文件夹结构发生更改,因此/views/shared 文件夹(或除 wwwroot 之外的任何项目文件夹)不存在。我认为azure只是构建项目,因为有一个views.dll文件;但现在我不知道如何将这些文件名设置为模型中的变量。

有没有办法在部署后迭代我的/views/shared 文件夹?

最佳答案

after deploying to Azure, the folder structure changes so that the /views/shared folder (or any project folders other than wwwroot) doesn't exist. I think azure is just building the project as there is a views.dll file; but now I don't know how to set these filenames into variables in my model.

您可以尝试将 CopyRazorGenerateFilesToPublishDirectory 属性设置为 true,这将有助于将 RazorGenerate 项目 (.cshtml) 文件复制到发布目录。

<PropertyGroup>

  <!--other properties here-->

  <CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
  
  <!--other properties here-->

</PropertyGroup>

有关更多信息,请查看以下文档:

https://learn.microsoft.com/en-us/aspnet/core/razor-pages/sdk?view=aspnetcore-5.0#properties

enter image description here

关于c# - 访问在 azure 服务器上运行的 .net core 应用程序中的共享 View 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66180125/

相关文章:

c# - 如何为没有标题栏的表单制作自定义边框?

azure - 如何有效地防止事实表中出现重复行?

azure - "www-authenticate →Bearer"与未经授权 401

iis - Asp.net Core Web API-当前用户和Windows身份验证

c# - 手动将 MVC 操作重定向到身份服务器 4 登录页面

c# - 如何通过代码在一个实例中创建 .NET 程序的另一个实例?

c# - 如何通过代码配置 ASP.Net 成员资格提供程序?

c# - 当其他用户的命令完成时,保留用户命令 ASP.Net

c# - 网站 gridview 无法在 azure 上的 iis 上工作

c# - SecurityTokenInvalidAudienceException : IDX10214: Audience validation failed