图像不在ASP.NET MVC中显示

时间:2016-03-11 00:16:37

标签: asp.net-mvc

ASP.NET MVC申请中,file已成功保存到文件夹,其URL已保存到SQL database。使用此file在文件夹中的浏览器中加载URL时出现问题。代码实现是:

[HttpPost]
    [ActionName("UploadPhoto")]
    public ActionResult UploadPhoto(HttpPostedFileBase photoPath)
    {
         var fileName = Path.GetFileName(photoPath.FileName);
        string path;
        if (photoPath.ContentLength > 0)
        {
            path = Path.Combine(Server.MapPath("~/Images/photos"), fileName);
            photoPath.SaveAs(path);
        return RedirectToAction("CreateWithImage", new { path = path });
        }
return View();
    }

public ActionResult CreateWithImage(string path)
    {
        employee em = new employee();
        em.districts = new SelectList(hc.districts, "name", "name");
        em.photoPath = path;
        return View(em);
    }

file(image)在视图中呈现为:

@model HRMS.Models.employee
<dd>
        @Html.Image(@Model.photoPath)
    </dd>

@Html.Image的扩展方法实现是:

    namespace HRMS.CustomeHTMLHelper
{
    public static class CustomHtmlHelper
    {
        public static IHtmlString Image(this HtmlHelper helper,string src)
        {
            TagBuilder tb = new TagBuilder("img");
            tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));
            return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
        }
    }
}

调用View时,我看到图像的链接断开。加载页面(View)的HTML(具有正确的文件路径)被视为:

<dd>                
            <img src="/App_Data/photos/1.png" />
            </dd>

当我尝试在浏览器中运行图像的物理路径,即<b>Requested URL</b> http://localhost:57852/App_Data/photos/1.png时,它会抛出HTTP Error 404.8 - Not Found错误。

我哪里错了?请帮忙。

0 个答案:

没有答案