![](/img/trans.png)
[英]ASP.NET Core - Unable to signIn with signInManager when app is published
[英]Asp.NET Core, unable to find View of cshtml when published
我在我的 Asp.NET CORE 服务中使用RazorEngine
,它生成动态 html 页面,这些页面将通过 EMAIL 从存储在 View 文件夹中的 cshtml 文件发送。
问题是,当网站发布时,服务无法找到Views/Emails/EmailRiepilogo.cshtml
的路径,因为发布时甚至没有创建 View 文件夹......
那么如何在服务发布时引用EmailRiepilogo.cshtml 路径呢?
这是我读取文件的方式:
string template = File.ReadAllText("Views/Emails/EmailRiepilogo.cshtml");
这是错误:
找不到路径'E:\VS2019\Project\bin\Debug\netcoreapp3.1\publish\Views\Emails\EmailRiepilogo.cshtml'.
我应该将 static 文件创建为 cshtml 吗?
您不知道您的应用程序将托管在何处(就主机服务器中的文件夹结构而言)。 同时,像File.ReadAllText
这样的方法在使用绝对路径时效果最好。 您需要将相对路径“Views/Emails/EmailRiepilogo.cshtml”转换为绝对路径。 一种方法是在 Asp.Net Core 中使用IHostingEnvironment
(或在 Asp.Net 中使用Server.MapPath
)。 只需将其注入您的服务/控制器,然后
string filename = Path.Combine(_hostingEnv.ContentRootPath, "Views/Emails/EmailRiepilogo.cshtml");
string template = File.ReadAllText(filename);
当然,您需要在发布项目后检查文件是否存在于您期望的位置。
我已经解决了通过将每个 cshtml 文件编译操作设置为Integrated Resource
并将复制到输出目录Copy always
到这样的方式来生成带有 cshtml 文件的视图文件夹并正确读取文件。
我有完全相同的问题。 Cshtml 文件构建操作以某种方式从内容 -> 无更改。 它在本地工作,但在发布到服务器时不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.