【问题标题】:Returning error message/page for FileResult in ASP.NET MVC在 ASP.NET MVC 中返回 FileResult 的错误消息/页面
【发布时间】:2013-10-08 06:47:57
【问题描述】:

我正在使用

<img src="@Url.Action("getImage", "AccessFile", new { imagePath= @ViewData["imagePath"] })"/>

在视图中显示作为本地文件存在于服务器中的图像。
AccessFileController 中的 getImage 操作看起来像

public ActionResult getImage(string imagePath)
{ 
    if (!System.IO.File.Exists(imagePath))
    {
        //Log
        return ?????????
    }
    else 
    {
        return base.File(imagePath, "image/jpg");
    }

}

我的问题是,我应该输入什么?????????允许用户看到某种错误信息?
我试过了

  • return new HttpNotFoundResult();
  • return RedirectToAction("ExpectedError", "Error");

但它只返回带有损坏图标的空图像。
有什么好的解决办法吗?

【问题讨论】:

  • 这不是文件结果,actionresult 的解决方案对 actionresult 不起作用

标签: asp.net-mvc exception-handling error-handling actionresult


【解决方案1】:

您可以为空请求显示预定义的图像,

return Content(imagePath2);

或者你可以显示一个javascript,然后你可以显示一个错误

    return Content("<script language='javascript' type='text/javascript'>alert('Image not found!');</script>");            

【讨论】:

  • 谢谢!我认为这是一个很好的解决方案!
  • 您不必在 C# 或 html 中使用 javascript!你也总是发送例外!不是错误代码或东西。以及写入和异常处理程序。
  • 我没有看到发送 js 从代码中查看有什么问题,为什么您希望用户看到异常,它应该登录到您的后端,无论如何显示错误代码是常见的做法.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
相关资源
最近更新 更多