我有以下方法来获取项目中的图像路径
public ActionResult RenderImage(string imageid, string pathvalue)
{
try
{
var URL = System.Configuration.ConfigurationManager.AppSettings[pathvalue].ToString();
var path = Path.Combine(URL, imageid + ".jpg");
return base.File(path, "image/jpeg");
}
catch (Exception)
{
throw;
}
}
我想把这个返回的Image路径作为字符串,为此我尝试了类似这样的东西
ControllerName ns = new ControllerName ();
var path = ns .RenderImage("id", "path");
string imageurl = path.ToString();
但这并没有将图像路径视为“C:/User/Data/Image.jpg”,其获取值为“System.Web.Mvc.FilePathResult”
如何解决这个问题
答案 0 :(得分:3)
尝试:
var ns = new ControllerName ();
var path = ns.RenderImage("id", "path");
var fileResult = path as FilePathResult;
if (fileResult != null)
{
string imageurl = fileResult.FileName;
imageurl = imageurl.Replace(@"\\", @"\");
}