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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With