【问题标题】:MVC 4 Images With Spaces In Name Not Displaying名称中带有空格的 MVC 4 图像不显示
【发布时间】:2013-12-07 15:22:50
【问题描述】:

我有一个 MVC 4 站点,其中有很多图像,其中包含无法轻易更改的空格。问题是该站点没有显示任何带有空格的图像,可能是因为该空间正在转换为 %20。我尝试在 web.config 文件中设置 httpruntime 的 invalidCharacters 属性以排除 % 没有运气。有谁知道解决此问题的好方法,或者可能是什么原因造成的?我曾在其他 MVC 站点工作过,以前从未遇到过这个问题。提前致谢!

我的图片标签/路径如下所示:

<img src="@Model.Picture.Location?width=230&height=285&bgcolor=222425" alt="@Html.DisplayFor(model => model.GameName)"/>

其中 Model.Picture.Location 是图片在 Web 服务器上的位置。

【问题讨论】:

  • 我们能看到生成的&lt;img&gt;标签吗?您是否引用了src= 属性?
  • 如何提供图片源路径..
  • 我编辑了这篇文章,展示了我如何放入 src 属性以及如何制作 标签的示例。

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 iis razor


【解决方案1】:

你能不能试试:

 <img src="@(Html.Raw(Model.Picture.Location))?width=230&height=285&bgcolor=222425" alt="@Html.DisplayFor(model => model.GameName)"/>

Html.Raw() 确保您的字符串未编码。

【讨论】:

  • 运气不好,它仍然给出 404。说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、更改名称或暂时不可用.请查看以下 URL 并确保其拼写正确。请求的网址:/BoxArt/GTA%20V.jpg
【解决方案2】:

问题是我正在替换 Global.asax.cs 中的 Application_BeginRequest 中的 url,并且没有正确处理 %20。我使用以下代码解决了我的 Application_BeginRequest 方法的问题。

     protected void Application_BeginRequest(object sender, EventArgs e)
     {
        string path = HttpContext.Current.Request.Url.PathAndQuery;
        path = HttpUtility.UrlDecode(path);
        if (!String.IsNullOrEmpty(path))
        {
           HttpContext.Current.RewritePath(path);
        }
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多