如何显示在 Asp.net MVC 中找不到的图像

发布于 2024-10-20 07:08:02 字数 288 浏览 5 评论 0原文

我有一个 ASP.NET MVC 项目。在特定视图中,我将来自项目内不同文件夹的四个图像称为。

有时找不到图像。我想为每个文件夹设置一个图像,因为所有 4 个文件夹都包含不同大小的图像,所以我需要为每个文件夹设置特定大小的图像。

当图像找不到任何方法时,我如何显示默认图像。我的意思是,当目录中没有我在视图中调用的图像时,调用 none.png 。

在找不到图像的情况下他们有什么方式显示图像。

在 asp.net 4 MVC 3. 中使用 web.config 或设置其他任何内容可以实现此目的。

I have a asp.net mvc project. in a speicific views i call the four image from different different folder inside my proect.

sometime image can not be found. i want to set a image for every folder because all 4 folder contain different diffent size of image so i need to set the image of specific size for each folder.

how i can show the default image when the image not found any way to do that.i means call the none.png when the directory not have image who i called in the views.

are their any way to show the image in the case of image not found.

any way to do that in asp.net 4 MVC 3. using web.config or setting anything else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生生漫 2024-10-27 07:08:03

您可以使控制器操作返回 FileResult,并接受描述图像路径的 string 类型的参数。

控制器操作尝试从磁盘加载图像并将其作为 FileResult 返回,或者如果磁盘中缺少该文件,则返回占位符“未找到图像”图像。

然后,要显示图像(或其占位符),您需要将 img 元素的 src 属性设置为控制器操作,而不是直接设置图像。

You can make a controller action return a FileResult, and accept a parameter of type string which describes the image path.

The controller action tries to load the image from disk and return it as a FileResult, or if the file is missing from the disk, return a place holder 'image not found' image instead.

Then to display the image (or its placeholder) you would set the src attribute of your img element to the controller action rather than the image directly.

や三分注定 2024-10-27 07:08:02

最简单的方法是使用 html 助手。请注意在显示图像文件名之前检查文件系统会对性能造成额外影响。不过,点击量很小,因此除非流量非常高,否则您不会注意到任何问题。然后,您可以实现某种缓存,以便应用程序“知道”文件是否存在。

您可以为其使用自定义 html 帮助程序

public static class ImageHtmlHelpers
{
   public static string ImageUrlFor(this HtmlHelper helper, string contentUrl)
   {
       // Put some caching logic here if you want it to perform better
       UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
       if (!File.Exists(helper.ViewContext.HttpContext.Server.MapPath(contentUrl)))
       {
           return urlHelper.Content("~/content/images/none.png");
       }
       else
       {
           return urlHelper.Content(contentUrl);
       }
   }
}

然后在您看来,您可以使用以下命令创建 url:

<img src="<% Html.ImageUrlFor("~/content/images/myfolder/myimage.jpg"); %>" />

编辑: 正如 Jim 指出的那样,我还没有真正解决大小调整问题。就我个人而言,我使用自动大小请求管理/大小,这是一个完全不同的故事,但如果您担心文件夹/大小,只需将该信息传递到构建路径即可。如下所示:

public static class ImageHtmlHelpers
{
   public static string ImageUrlFor(this HtmlHelper helper, string imageFilename, ImageSizeFolderEnum imageSizeFolder)
   {
       UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
       string contentUrl = String.Format("~/content/userimages/{0}/{1}", imageSizeFolder, imageFilename);
       if (!File.Exists(helper.ViewContext.HttpContext.Server.MapPath(contentUrl)))
       {
           return urlHelper.Content(String.Format("~/content/userimages/{0}/none.png", imageSizeFolder));
       }
       else
       {
           return urlHelper.Content(contentUrl);
       }
   }
}

然后在您看来,您可以使用以下方式创建网址:

<img src="<% Html.ImageUrlFor("myimage.jpg", ImageSizeFolderEnum.Small); %>" />

如果文件夹是固定集,建议使用枚举来更好地进行编程控制,但为了一种快速而令人讨厌的方法,而不是您不能只使用字符串的原因如果该文件夹是数据库生成的等。

Easiest way to do this is with a html helper. Please be mindful of the extra performace hit of checking the file system before even showing an image filename. The hit is a small one though, so you won't notice any issues unless you are getting very high traffic. Then you can implement some sort of caching so the app "knows" if the file exists or not.

You can use a custom html helper for it

public static class ImageHtmlHelpers
{
   public static string ImageUrlFor(this HtmlHelper helper, string contentUrl)
   {
       // Put some caching logic here if you want it to perform better
       UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
       if (!File.Exists(helper.ViewContext.HttpContext.Server.MapPath(contentUrl)))
       {
           return urlHelper.Content("~/content/images/none.png");
       }
       else
       {
           return urlHelper.Content(contentUrl);
       }
   }
}

Then in your view you can just make the url using:

<img src="<% Html.ImageUrlFor("~/content/images/myfolder/myimage.jpg"); %>" />

EDIT: As Jim pointed out, I haven't really addressed the sizing issue. Personally I use automatic size request management/size which is a whole other story, but if you are concerned about the folders/sizes simply pass that information in to build the path. As below:

public static class ImageHtmlHelpers
{
   public static string ImageUrlFor(this HtmlHelper helper, string imageFilename, ImageSizeFolderEnum imageSizeFolder)
   {
       UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
       string contentUrl = String.Format("~/content/userimages/{0}/{1}", imageSizeFolder, imageFilename);
       if (!File.Exists(helper.ViewContext.HttpContext.Server.MapPath(contentUrl)))
       {
           return urlHelper.Content(String.Format("~/content/userimages/{0}/none.png", imageSizeFolder));
       }
       else
       {
           return urlHelper.Content(contentUrl);
       }
   }
}

Then in your view you can just make the url using:

<img src="<% Html.ImageUrlFor("myimage.jpg", ImageSizeFolderEnum.Small); %>" />

Have suggested an Enum for better programmatic control if the folder is a fixed set, but for a quick and nasty approach, not reason why you can't just use a string if the folder is db generated etc.

夜血缘 2024-10-27 07:08:02

我不太了解 MVC,但我的想法仍然是:

有一个特定的控制器,它可以根据您的逻辑获取图像。就像迈克尔说的那样,在成功审核后返回一些东西(图像) - 意思是如果找到图像。如果未找到,则返回状态代码 404 或类似的内容。

通过客户端上的 javascript 处理其余部分。所以在img标签中为onerror事件写一些东西。可能会用找不到海报图像文件的图像替换 img 源。这里有一些(但不完全)如何通过 jquery 做到这一点。

$(function(){
    $('#myimg').error(function(){
        $('#result').html("image not found");
    });
});

PS:http://jsfiddle.net/Fy9SL/2/ 如果您对完整演示感兴趣jquery 代码。

Don't know all that much MVC, but here is my idea nonetheless:

Have a specific controller which fetches an image based on whatever your logic. Like Michael said have something (image) returned on a successful audit - meaning if image is found. If not found return a status code 404 or something like that.

Handle the rest via javascript on the client. So in the img tag write something for the onerror event. May be replace the img source w/ your image not found poster image file. Here somewhat (but not exactly) how to do that via jquery.

$(function(){
    $('#myimg').error(function(){
        $('#result').html("image not found");
    });
});

PS: http://jsfiddle.net/Fy9SL/2/ if u are interested in the full demo of jquery code.

烈酒灼喉 2024-10-27 07:08:02
    public static MvcHtmlString Image(this HtmlHelper helper, string src, string alt)
    {
        var builder = new TagBuilder("img");

        // Put some caching logic here if you want it to perform better
        UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        if (!File.Exists(helper.ViewContext.HttpContext.Server.MapPath(src)))
        {
            src = urlHelper.Content("~/content/images/none.png");
        }
        else
        {
            src = urlHelper.Content(src);
        }

        builder.MergeAttribute("src", src);
        builder.MergeAttribute("alt", alt);
        return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
    }
    public static MvcHtmlString Image(this HtmlHelper helper, string src, string alt)
    {
        var builder = new TagBuilder("img");

        // Put some caching logic here if you want it to perform better
        UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        if (!File.Exists(helper.ViewContext.HttpContext.Server.MapPath(src)))
        {
            src = urlHelper.Content("~/content/images/none.png");
        }
        else
        {
            src = urlHelper.Content(src);
        }

        builder.MergeAttribute("src", src);
        builder.MergeAttribute("alt", alt);
        return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文