排除/忽略图像 ImageUrl 中的参数,但仍将其显示在中继器中

发布于 2024-12-24 19:43:19 字数 1018 浏览 1 评论 0原文

我在中继器内有一个图像控件。我想排除/忽略 imageurl 属性中的参数,因为路径(源图像)的名称中没有此参数,但我仍然想在 url 中显示该参数。我无法重命名所有图像,因为图像数量很多。希望它有意义。

这是一个例子。

这是为了搜索引擎优化。原始路径

  • /product/0001_100_00_KK00_F02.png

我想在 imageurl

  • /product/0001_100_00_KK00_F02.png/Tv-Sony-lcd-black-bravia-KDL-26V4500

中显示的内容或

  • /product/0001_100_00_KK00_F02/Tv-Sony-lcd-black-bravia-KDL-26V4500.png
 
        <项目模板>
            
        
    

和代码隐藏

 rptImages.DataSource = Images.Select(s => new { Url = s });
    rptImages.DataBind();

这对于原始路径来说效果很好。但是当我将 /Tv-Sony-lcd-black-bravia-KDL-26V45000 添加到网址末尾时,找不到图像(当然)。

那么如何向 imageurl 添加额外的参数,但让图像控件忽略此参数并仍然找到图像路径呢?

有谁知道如何解决这个问题

I have an image control inside a repeater. I would like to exclude/ignore a parameter in the imageurl property, because the path(source image) do not have this parameter in the name, but I still want to show the parameter in url. And I can't rename all the images, as there are a lot. Hope it make sense.

Here is an example.

It is for SEO. Original path

  • /product/0001_100_00_KK00_F02.png

What I want to show in imageurl

  • /product/0001_100_00_KK00_F02.png/Tv-Sony-lcd-black-bravia-KDL-26V4500

OR

  • /product/0001_100_00_KK00_F02/Tv-Sony-lcd-black-bravia-KDL-26V4500.png
    <asp:Repeater ID="rptImages" runat="server">
        <ItemTemplate>
            <asp:Image ID="Image1" ImageUrl='<%# Eval("Url") %>' runat="server" />
        </ItemTemplate>
    </asp:Repeater>

And codebehind

    rptImages.DataSource = Images.Select(s => new { Url = s });
    rptImages.DataBind();

This works fine for the original path. But when I add /Tv-Sony-lcd-black-bravia-KDL-26V45000 to the end of url the images is not found(of course).

So how do I add an extra parameter to the imageurl, but get the image control to ignore this and still find the image path?

Does anyone have any ideas of how to resolve this problem

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

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

发布评论

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

评论(2

葬シ愛 2024-12-31 19:43:19

您可以通过 URL 重写来实现这一点。将 global.asax 文件添加到您的项目中并使用类似这样的某种重写逻辑。

    void Application_BeginRequest(Object sender, EventArgs e)
{
    string currentPath;
    currentPath = Request.Path; // /product/0001_100_00_KK00_F02.png/Tv-Sony-lcd-black-bravia-KDL-26V4500
    if (currentPath.IndexOf(".png") > -1)
    {
        string[] paths = currentPath.Split('/');
        currentPath = currentPath.Replace("/" + paths[paths.Length - 1], ""); // /product/0001_100_00_KK00_F02.png
        Context.RewritePath(currentPath);
    }
}

这可以工作,但也会重写所有包含“.png”的 request.paths,因为“if (currentPath.IndexOf(“.png”) > -1)”
多加一些调理会更安全。

You can achive that with URL rewriting. Add global.asax file to your project and use some kind of rewriting logic like this.

    void Application_BeginRequest(Object sender, EventArgs e)
{
    string currentPath;
    currentPath = Request.Path; // /product/0001_100_00_KK00_F02.png/Tv-Sony-lcd-black-bravia-KDL-26V4500
    if (currentPath.IndexOf(".png") > -1)
    {
        string[] paths = currentPath.Split('/');
        currentPath = currentPath.Replace("/" + paths[paths.Length - 1], ""); // /product/0001_100_00_KK00_F02.png
        Context.RewritePath(currentPath);
    }
}

This will work but will also rewrite all request.paths which contains".png" because of "if (currentPath.IndexOf(".png") > -1)"
It is safer to include few more conditioning.

颜漓半夏 2024-12-31 19:43:19

像这样的 URL 有效吗?

/product/0001_100_00_KK00_F02.png?Tv-Sony-lcd-black-bravia-KDL-26V4500

只要您在文件名后面加上问号,您的图像就会正常显示。正如@Jon Edgerton 指出的,IIS 没有找到您的图像,这就是它不显示的原因。

如果您想要一个完全自定义的 URL,那么您将必须进行一些 URL 重写。这是一篇关于该内容的文章: http:// /learn.iis.net/page.aspx/517/url-rewriting-for-aspnet-web-forms/

Would a URL like this work?

/product/0001_100_00_KK00_F02.png?Tv-Sony-lcd-black-bravia-KDL-26V4500

As long as you put a question mark after the file name, your image will display just fine. As @Jon Edgerton points out, IIS is not finding your image and that is why it doesn't display.

If you want a completely custom URL, then you are going to have to do some URL re-writing. Here is an article on that: http://learn.iis.net/page.aspx/517/url-rewriting-for-aspnet-web-forms/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文