How to Replace URLs in Displayed HTML Using ASP.NET MVC ActionFilter

I am trying to create an ActionFilter to replace some text in my HTML. Basically, when the server uses SSL, I want to replace the links to my CDN (http://cdn.example.com) with the links directly to my server (https://www.example.com). So, the structure is something like this (I assume that OnResultExecuted , where should I start):

public class CdnSslAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if(filterContext.HttpContext.Request.IsSecureConnection)
        {
            // when the connection is secure,
            // somehow replace all instances of http://cdn.example.com
            // with https://www.example.com
        }
    }
}

This will be used in my secure controllers:

[CdnSsl] 
public class SecureController : Controller
{
}

The reason I want to do this is because my CDN does not support SSL. And there are links on the main pages to CDN resources. Example:

<link href="http://cdn.example.com/Content/base.css" rel="stylesheet" type="text/css" />
+3
source share
4

:

http://arranmaclean.wordpress.com/2010/08/10/minify-html-with-net-mvc-actionfilter/

:

public class CdnSslAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsSecureConnection)
        {
            var response = filterContext.HttpContext.Response;
            response.Filter = new CdnSslFilter(response.Filter);
        }
    }
}

( ):

public class CdnSslFilter : Stream
{
    private Stream _shrink;
    private Func<string, string> _filter;

    public CdnSslFilter(Stream shrink)
    {
        _shrink = shrink;
        _filter = s => Regex.Replace(s,@"http://cdn\.","https://www.", RegexOptions.IgnoreCase);
    }

    //overridden functions omitted for clarity. See above blog post.

    public override void Write(byte[] buffer, int offset, int count)
    {
        // capture the data and convert to string 
        byte[] data = new byte[count];
        Buffer.BlockCopy(buffer, offset, data, 0, count);
        string s = Encoding.Default.GetString(buffer);

        // filter the string
        s = _filter(s);

        // write the data to stream 
        byte[] outdata = Encoding.Default.GetBytes(s);
        _shrink.Write(outdata, 0, outdata.GetLength(0));
    }
}
+5

, @Haacked question .

0

.

( -) Html ( Html.Content()), URL-. , , , , , Request.Items, .

0

, @marcind, - URL- URL-.

public static MvcHtmlString CdnActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName)
{
    if(helper.ViewContext.HttpContext.Request.IsSecureConnection)
    {
        return helper.ActionLink(linkText, actionName, controllerName, "https", "www.yourhost.com"...);
    }
    return helper.ActionLink(linkText, actionName, controllerName);
}

One of the drawbacks of this approach is that you will need to replace all your current calls ActionLinkin your views (or at least the ones you need) with the call to this extension method.

0
source

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


All Articles