I have a class libary project (NotificationTemplate) which return content of file:
我有一个类库项目(NotificationTemplate),它返回文件的内容:
public static class Template
{
public static string NotificatioEmail
{
get { return File.ReadAllText("Templates\\NotificatioEmail.cshtml"); }
}
}
In this project is located folder Templates
with NotificatioEmail.cshtml
file.
在这个项目中找到带有NotificatioEmail.cshtml文件的文件夹模板。
Also, I have two application: console app and ASP.NET MVC app. The file returned fine in the console app, but in the MVC I get error file not found
. How to write universally?
I tryed this:
另外,我有两个应用程序:控制台应用程序和ASP.NET MVC应用程序。该文件在控制台应用程序中返回正常,但在MVC中我找不到错误文件。怎么写普遍?我试过这个:
Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Templates",
"NotificatioEmail.cshtml")
but BaseDirectory
don't include bin
folder.
但是BaseDirectory不包含bin文件夹。
1 个解决方案
#1
35
As you said BaseDirectory
works for console apps. For MVC applications, use RelativeSearchPath
instead.
正如您所说,BaseDirectory适用于控制台应用程序。对于MVC应用程序,请改用RelativeSearchPath。
So you can use this code for both platforms
因此,您可以在两个平台上使用此代码
var appDomain = System.AppDomain.CurrentDomain;
var basePath = appDomain.RelativeSearchPath ?? appDomain.BaseDirectory;
Path.Combine(basePath, "Templates", "NotificatioEmail.cshtml");
#1
35
As you said BaseDirectory
works for console apps. For MVC applications, use RelativeSearchPath
instead.
正如您所说,BaseDirectory适用于控制台应用程序。对于MVC应用程序,请改用RelativeSearchPath。
So you can use this code for both platforms
因此,您可以在两个平台上使用此代码
var appDomain = System.AppDomain.CurrentDomain;
var basePath = appDomain.RelativeSearchPath ?? appDomain.BaseDirectory;
Path.Combine(basePath, "Templates", "NotificatioEmail.cshtml");