ligershark / WebOptimizer

A bundler and minifier for ASP.NET Core
Apache License 2.0
765 stars 112 forks source link

Broken if app is deployed to IIS sub application ( virtual dir ) #73

Open dandros opened 6 years ago

dandros commented 6 years ago

In layout I put ~ to resolve app root.

It generates script tag without cache busting param.

iqmeta commented 6 years ago

same with miniblog https://github.com/madskristensen/Miniblog.Core/issues/40

mjharper84 commented 6 years ago

I'm running a test app on a sub domain, if the app has been idling and restarts I get 500s for my bundles (I have to refresh the page) guessing a similar issue?

JohnTrevorBurke commented 4 years ago

I fixed this temporally by adding a customer tag helper that extends the link tag helper. It adds a new attribute on the tag called "RenderRelative". If that attribute is true, it drops the leading '/' to fix for IIS sub apps. It would be nice if a more long term fix were included in the nuget project itself.

public class RelativeRouteLinkTagHelper : LinkTagHelper
{
    public RelativeRouteLinkTagHelper(IWebHostEnvironment env, IMemoryCache cache, IAssetPipeline pipeline, IOptionsMonitor<WebOptimizerOptions> options) : base(env, cache, pipeline, options)
    {
    }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        base.Process(context, output);
        output.Attributes.TryGetAttribute("RenderRelative", out var renderRelative);
        var isTrue = "true".Equals(renderRelative?.Value?.ToString(), StringComparison.CurrentCultureIgnoreCase);
        if (!isTrue) return;
        output.Attributes.TryGetAttribute("href", out var hrefAtr);
        var href = hrefAtr?.Value as string;
        if(!string.IsNullOrEmpty(href))
            output.Attributes.SetAttribute("href", href.TrimStart('/'));
    }
}