Michael Olesen
Michael Olesen

Reputation: 473

Change image route to external link

I have a route that returns a FileResult with an image.

View:

<img src="@Url.RouteUrl("HomeProductImage", new { id = Model.ProductId })" />

Route to the action:

routes.MapRouteLowercase("HomeProductImage", "{id}.jpg", new { controller = "Home", 
    action = "ProductImage", id = "" }, new { id = @"\d+" });

public FileResult ProductImage(int id)
{
     // ...
     return File(filename, "image/jpeg");
}

How can I change the route, to get an image from another domain, without changing the View?

Eg. instead of domain.com/product/123.jpg -> otherDoamin.com/product/123.jpg

Upvotes: 0

Views: 111

Answers (1)

Martin
Martin

Reputation: 1634

It's not possible, you should instead write a helper method that returns the full url for your image.

public static MvcHtmlString ImageUrl(this HtmlHelper html, string imageName)
{
    return String.Format("http://somedomain.com/path/to/image/{0}", imageName);
}

Upvotes: 1

Related Questions