扩展/替换Amazon S3(或其他CDN)的Html.Image

时间:2022-11-18 23:03:52

Just want to confirm that there is no way to extend or replace Html.Image functionality without writing a replacement function.

只是想确认无法在不编写替换函数的情况下扩展或替换Html.Image功能。

I want to write a function that will use Amazon's S3 service for hosting images.

我想编写一个将使用亚马逊的S3服务来托管图像的功能。

The best approach I've come up with is a helper method Html.SmartImage which would check a configuration property to see if i wanted to go to Amazon or not. It may even check a database of files that are hosted remotely and only 'farm them out' if they are in that list.

我提出的最好的方法是帮助方法Html.SmartImage,它将检查配置属性,看看我是否想要去亚马逊。它甚至可以检查远程托管的文件的数据库,如果它们在该列表中,则只将它们“移出”。

I'll post what I have when I've done it - but curious about any 'outside the box' ideas.

我会在发布时发布我所拥有的内容 - 但对任何“开箱即用”的想法感到好奇。

2 个解决方案

#1


No you are right, you need to create your own extension method to handle custom scenarios like this.

不,你是对的,你需要创建自己的扩展方法来处理这样的自定义方案。

#2


This is what I did - at least for now:

这就是我所做的 - 至少现在:

Search and replace:

搜索和替换:

 html.image("~

for

 Html.CDNImage("~

And then created a helper in a static class ImageExtensions :

然后在静态类ImageExtensions中创建一个帮助器:

public static string CDNImage(this HtmlHelper htmlHelper, string imageRelativeUrl)
        {
            return CDNImage(htmlHelper, imageRelativeUrl, null, null);
        }

        public static string CDNImage(this HtmlHelper htmlHelper, string imageRelativeUrl, object htmlAttributes)
        {
            return CDNImage(htmlHelper, imageRelativeUrl, null, htmlAttributes);

        }

        public static string CDNImage(this HtmlHelper htmlHelper, string imageRelativeUrl, string alt, object htmlAttributes)
        {
            string url = Regex.Replace(imageRelativeUrl, "~/content/", "http://s3.amazon.com/", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            return htmlHelper.Image(url, alt, htmlAttributes);

        }

Obviously I'm using s3.amazon.com as a placeholder here - you have to fill in the URL for your CDN whatever it is.

显然我在这里使用s3.amazon.com作为占位符 - 你必须填写你的CDN的URL,无论它是什么。

If required you can use some kind of configuration property to determine whether or not you actually do the replacement.

如果需要,您可以使用某种配置属性来确定您是否实际进行了替换。

#1


No you are right, you need to create your own extension method to handle custom scenarios like this.

不,你是对的,你需要创建自己的扩展方法来处理这样的自定义方案。

#2


This is what I did - at least for now:

这就是我所做的 - 至少现在:

Search and replace:

搜索和替换:

 html.image("~

for

 Html.CDNImage("~

And then created a helper in a static class ImageExtensions :

然后在静态类ImageExtensions中创建一个帮助器:

public static string CDNImage(this HtmlHelper htmlHelper, string imageRelativeUrl)
        {
            return CDNImage(htmlHelper, imageRelativeUrl, null, null);
        }

        public static string CDNImage(this HtmlHelper htmlHelper, string imageRelativeUrl, object htmlAttributes)
        {
            return CDNImage(htmlHelper, imageRelativeUrl, null, htmlAttributes);

        }

        public static string CDNImage(this HtmlHelper htmlHelper, string imageRelativeUrl, string alt, object htmlAttributes)
        {
            string url = Regex.Replace(imageRelativeUrl, "~/content/", "http://s3.amazon.com/", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            return htmlHelper.Image(url, alt, htmlAttributes);

        }

Obviously I'm using s3.amazon.com as a placeholder here - you have to fill in the URL for your CDN whatever it is.

显然我在这里使用s3.amazon.com作为占位符 - 你必须填写你的CDN的URL,无论它是什么。

If required you can use some kind of configuration property to determine whether or not you actually do the replacement.

如果需要,您可以使用某种配置属性来确定您是否实际进行了替换。