'asp-src-include' doesn't resolve files
Asked Answered
O

2

8

I'm building a webapp with ASP.Net Core, typescript, react and webpack. It's called Ui.WebApp. The React app is located at Ui.WebApp/ClientApp and the webpack build outputs to Ui.WebApp/ClientApp/dist. In my Startup.cs I've included

app.UseStaticFiles(new StaticFileOptions {
        FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "ClientApp/dist"))
    }
);

That works i.e. when I load styles in my _Layout.cshtml as <link rel="stylesheet" href="~/main.css" asp-append-version="true" type="text/css" /> which physically resides in Ui.WebApp/ClientApp/dist/main.css. It also works for scripts I include as <script src="~/main.js"></script>.

Now I need to build the scripts in webpack with a hash in their name, i.e. main_968495a262da1789981f.js. The pattern for that in ASP.Net is to use <script asp-src-include="~/main_*.js"></script>. However, when viewing the page from a browser, there is no resulting script-tag, as I assume ASP.Net does not find any file matching the pattern (but I can see it in a file explorer).

Strangely, when I manually put the hashed js file into Ui.WebApp/wwwroot (which I don't use otherwise), the script tags are filled in correctly. They even stay there when I delete the folder and refresh the page.

So I assume ASP.Net validates the tag-helpers on compiletime and thus can't see the configuration I do in the Startup.cs.

Is there any way to get ASP.Net to validate the tag-helpers during runtime and pick the correct location?

Obvious answered 3/4, 2019 at 15:17 Comment(2)
Nasto, did you ever figure this out? The solution below doesn't seem to work when serving static files from a custom FileProvider...Ore
The below proposed solution is also not working for me. Did you ever resolve this?Nureyev
A
2

I was just running into this, and I think I have it figured out. A lot of the examples out there show using the tilde, but if you look at the intellisense hint it says that it is relative to your Webroot settings. So you don't need the tilde, and in fact it seems to break it. This seems to be at least true for .net core 2.1

I finally got it working like this for your example :

<script asp-src-include="main_*.js"></script>

So this was my first attempt, which doesn't work:

<script type="text/javascript" asp-src-include="~/dist/**/*.js"></script>

But when changed to this, it does!

<script type="text/javascript" asp-src-include="dist/**/*.js"></script>

Hopefully this saves someone else some massive amounts of frustration.

Avifauna answered 31/7, 2019 at 21:10 Comment(0)
A
0

Anyone running into issues with asp-src-include should check that project has _ViewImports.cshmtl containing @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers. Can help if TagHelper doesn't work at all

Arsonist answered 18/9, 2022 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.