ASP.Net Combres and Combres MVC with Cloudfront

I am currently using Amazon Cloudfront to serve static objects in my ASP.Net MVC3 C # site. Thus, all static resources http://cdn.domainname.com/ are added before the resource.

At the same time, I use combres and combred mvc to compress and combine my CSS and Javascript.

The tag for outputting minimized combined files is as follows.

@Html.Raw(WebExtensions.CombresLink("siteCss")) @Html.Raw(WebExtensions.CombresLink("siteJs")) 

This creates links on my site for

 <link rel="stylesheet" type="text/css" href="/combres.axd/siteCss/-63135510/"/> <script type="text/javascript" src="/combres.axd/siteJs/-561397631/"></script> 

As you can see, my cloud cdn is not in front of it, so I do not get the benefits of the cloud interface with these files.

Is there anyone who knows how to insert my cdn without modifying the source code of the acll combress dll file?

+4
source share
2 answers

I am not familiar with Cloudfront, but with Combres (latest version) you can change the host name (which is added as a prefix before /combres.axd ... by setting the host attribute. For example:

  <resourceSets url="~/combres.axd" host="static.mysite.com" defaultDuration="365" defaultVersion="auto" defaultDebugEnabled="false" defaultIgnorePipelineWhenDebug="true" localChangeMonitorInterval="30" remoteChangeMonitorInterval="60" > 

Please let me know if this approach works with CloudFront?

+4
source

I ran into the same issue a few months ago and just ran into this post. I managed to get around this by creating my own Combres filter (FixUrlsInCSSFilter), which will read the “Base Url” value from the web.config file or database and apply it to all combres image URLs.

Hope this helps someone ...

combres.xml

 <combres xmlns='urn:combres'> <filters> <filter type="MySite.Filters.FixUrlsInCssFilter, MySite" /> </filters> 

FixUrlsInCssFilter - most of this file was copied from the original reflected file

  public sealed class FixUrlsInCssFilter : ISingleContentFilter { /// <inheritdoc cref="IContentFilter.CanApplyTo" /> public bool CanApplyTo(ResourceType resourceType) { return resourceType == ResourceType.CSS; } /// <inheritdoc cref="ISingleContentFilter.TransformContent" /> public string TransformContent(ResourceSet resourceSet, Resource resource, string content) { string baseUrl = AppSettings.GetImageBaseUrl(); return Regex.Replace(content, @"url\((?<url>.*?)\)", match => FixUrl(resource, match, baseUrl), RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.ExplicitCapture); } private static string FixUrl(Resource resource, Match match, string baseUrl) { try { const string template = "url(\"{0}\")"; var url = match.Groups["url"].Value.Trim('\"', '\''); while (url.StartsWith("../", StringComparison.Ordinal)) { url = url.Substring(3); // skip one '../' } if (!baseUrl.EndsWith("/")) baseUrl += "/"; if (baseUrl.StartsWith("http")) { return string.Format(CultureInfo.InvariantCulture, template, baseUrl + url); } else return string.Format(CultureInfo.InvariantCulture, template, (baseUrl + url).ResolveUrl()); } catch (Exception ex) { // Be lenient here, only log. After all, this is just an image in the CSS file // and it should't be the reason to stop loading that CSS file. EventManager.RaiseExceptionEvent("Cannot fix url " + match.Value, ex); return match.Value; } } } #region Required to override FixUrlsInCssFilter for Combres public static class CombresExtensionMethods { /// <summary> /// Returns the relative HTTP path from a partial path starting out with a ~ character or the original URL if it an absolute or relative URL that doesn't start with ~. /// </summary> public static string ResolveUrl(this string originalUrl) { if (string.IsNullOrEmpty(originalUrl) || IsAbsoluteUrl(originalUrl) || !originalUrl.StartsWith("~", StringComparison.Ordinal)) return originalUrl; /* * Fix up path for ~ root app dir directory * VirtualPathUtility blows up if there is a * query string, so we have to account for this. */ var queryStringStartIndex = originalUrl.IndexOf('?'); string result; if (queryStringStartIndex != -1) { var baseUrl = originalUrl.Substring(0, queryStringStartIndex); var queryString = originalUrl.Substring(queryStringStartIndex); result = string.Concat(VirtualPathUtility.ToAbsolute(baseUrl), queryString); } else { result = VirtualPathUtility.ToAbsolute(originalUrl); } return result.StartsWith("/", StringComparison.Ordinal) ? result : "/" + result; } private static bool IsAbsoluteUrl(string url) { int indexOfSlashes = url.IndexOf("://", StringComparison.Ordinal); int indexOfQuestionMarks = url.IndexOf("?", StringComparison.Ordinal); /* * This has :// but still NOT an absolute path: * ~/path/to/page.aspx?returnurl=http://www.my.page */ return indexOfSlashes > -1 && (indexOfQuestionMarks < 0 || indexOfQuestionMarks > indexOfSlashes); } } #endregion 

The AppSettings class is for retrieving a value from web.config. I also use this to create a path for images that are not processed by combres ...

  public class AppSettings { /// <summary> /// Retrieves the value for "ImageBaseUrl" if the key exists /// </summary> /// <returns></returns> public static string GetImageBaseUrl() { string baseUrl = ""; if (ConfigurationManager.AppSettings["ImageBaseUrl"] != null) baseUrl = ConfigurationManager.AppSettings["ImageBaseUrl"]; return baseUrl; } } 

web.config values

 <appSettings> <add key="ImageBaseUrl" value="~/Content/Images/" /> <!--For Development--> <add key="ImageBaseUrl" value="https://d209523005EXAMPLE.cloudfront.net/Content/Images/" /> <!--For Production--> </appSettings> 
+1
source

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


All Articles