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