use*_*173 17 c# asp.net-mvc class-library
我有一个类库项目(NotificationTemplate),它返回文件的内容:
public static class Template
{
public static string NotificatioEmail
{
get { return File.ReadAllText("Templates\\NotificatioEmail.cshtml"); }
}
}
Run Code Online (Sandbox Code Playgroud)
在这个项目中找到Templates带NotificatioEmail.cshtml文件的文件夹.
另外,我有两个应用程序:控制台应用程序和ASP.NET MVC应用程序.该文件在控制台应用程序中返回正常,但在MVC中我收到错误file not found.怎么写普遍?
我试过这个:
Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Templates",
"NotificatioEmail.cshtml")
Run Code Online (Sandbox Code Playgroud)
但BaseDirectory不包括bin文件夹.
p.s*_*w.g 40
正如你所说的BaseDirectory适用于控制台应用.对于MVC应用程序,请RelativeSearchPath改用.
因此,您可以在两个平台上使用此代码
var appDomain = System.AppDomain.CurrentDomain;
var basePath = appDomain.RelativeSearchPath ?? appDomain.BaseDirectory;
Path.Combine(basePath, "Templates", "NotificatioEmail.cshtml");
Run Code Online (Sandbox Code Playgroud)