How to maintain CDN image links on dev and production sites?

I have this ASP.NET MVC application that I now want to highlight links to a CDN image. At this point, images usually have this markup code:

<%= Html.ActionLinkWithImage("/Content/Img/Image1.png" ...

This works great when developing using a local web server (Cassini), as well as when publishing a prod server.
But now I want the production URL to look something like this:

http://cdn.com/Img/Image1.png

How can I make this work in a simple way so that it works without problems in dev and after publishing to prod?

+3
source share
2 answers

URL, , Web.config( - ), URL- URL-.

public static MvcHtmlString CDNImageLink(this UrlHelper url, string imageName)
{
    string urlFormat;
    if((bool)ConfigurationManager.AppSettings["Debug"])
        urlFormat = "/Content/Img/{0}";
    else
        urlFormat = "http://cdn.com/Img/{0}";

    return MvcHtmlString.Create(string.Format(urlFormat, imageName));
}

, URL- ( HTML):

public static MvcHtmlString ActionLinkWithImage(this HtmlHelper html, string imageName)
{
     UrlHelper Url = new UrlHelper(html.ViewContext.RequestContext);
     string imageUrl = Url.CDNImageLink(imageName);
     // generate the rest of the ActionLink using the imageUrl
}

, using , Url , HTML, .

+3

, cdn IP-. , static.myresources.com localhost , html .

0

Source: https://habr.com/ru/post/1762071/


All Articles