ASP.NET MVC Dynamically Generated Image URLs

I have an ASP.NET MVC application where I display images.

These images can be located on the file system or inside the database. This is great because I can use Url.Action in my image, trigger an action on my controller and return the image from the appropriate location.

However, I want to be able to support images stored in Amazon S3. In this case, I do not want my controller action to return an image, it should instead create an image URL for Amazon S3.

Although I could just execute this logic in my view, for example.

<% if (Model.Images [0] .ImageLocation == ImageLocation.AmazonS3) {%> // render the Amazon image

I need to make sure that the image exists first.

Essentially, I need to pass the size value to my controller so that I can verify that the image exists at that size (whether in the database, file system, or Amazon s3). As soon as I am sure that the image exists, I return the URL.

Hope that makes sense

Ben

+5
source share
3 answers

Try the following approach.

The model class for the image tag.

public class ImageModel
{
    public String Source { get; set; }
    public String Title { get; set; }
}

Assistant

public static String Image(this HtmlHelper helper, String source, String title)
{
    var builder = new TagBuilder("img");
    builder.MergeAttribute("src", source);
    builder.MergeAttribute("title", title);
    return builder.ToString();
}

View from Model.ImagestypeIEnumerable<ImageModel>

...    
<%= Html.Image(Model.Images[0].Source, Model.Images[0].Title) %>

Act

public ActionResult ActionName(/*whatever*/)
{
    // ...
    var model = ...;
    //...

    var model0 = ImageModel();
    if (Image0.ImageLocation == ImageLocation.AmazonS3)
        model0.Source = "an amazon url";
    else
        model0.Source = Url.Action("GetImageFromDatabaseOrFileSystem", "MyController", new { Id = Image0.Id });
    model0.Title = "some title";
    model.Images.Add(model0);
    // ...
    return View(model);
}

An action is a kind of pseudo-code, but the idea must be clear.

+2
source

, .

URL- . :

        products.ForEach(p =>
        {
            p.Images[0].Url = _mediaService.GetImageUrl(p.Images[0], 200);
        });

, , . , , Product.FullSizeImageUrl, Product.ThumbnailImageUrl.

"", . , , Amazon S3.

- , , URL- .

, RenderAction ASP.NET MVC ​​ :

:

    [ChildActionOnly]
    public ActionResult CatalogImage(CatalogImage image, int targetSize)
    {
        image.Url = _mediaService.GetImageUrl(image, targetSize);
        return PartialView(image);
    }

:

         public MediaCacheLocation CacheLocation { get; set; }

     public string GetImageUrl(CatalogImage image, int targetSize)
     {
         string imageUrl;

         // check image exists
         // if not exist, load original image from store (fs or db)
         // resize and cache to relevant cache location

         switch (this.CacheLocation) {
             case MediaCacheLocation.FileSystem:
                 imageUrl = GetFileSystemImageUrl(image, targetSize);
                 break;
             case MediaCacheLocation.AmazonS3:
                 imageUrl = GetAmazonS3ImageUrl(image, targetSize);
                 break;
             default:
                 imageUrl = GetDefaultImageUrl();
                 break;
         }

         return imageUrl;
     }

Html helper:

        public static void RenderCatalogImage(this HtmlHelper helper, CatalogImage src, int size) {
        helper.RenderAction("CatalogImage", "Catalog", new { image = src, targetSize = size });
    }

:

<%Html.RenderCatalogImage(Model.Images[0], 200); %>

, Amazon S3.

url, , URL- SSL/ - VirtualPathUtility.

+1

HttpWebRequest . , 200 , , - .

-1

All Articles