UPDATED ANSWER
Thanks to Shoe I learned about the existence of environment tag helpers, so instead of my old answer where I achieved the same manually, we can now use it like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"]</title>
<environment include="Development">
<link rel="stylesheet" href="~/css/sasw.css" asp-append-version="true" />
</environment>
<environment exclude="Development">
<!-- Google Analytics here for example -->
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>
</head>
OLD ANSWER
A solution for Razor pages that should also work with Razor views (dotnet core 3.1+) could be to inject the IWebHostEnvironment
and to render conditionally depending on the current environment (defined by the global variable ASPNETCORE_ENVIRONMENT
)
At your _Layout.cshtml
@using Microsoft.Extensions.Hosting
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment Env
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"]</title>
@if (Env.IsDevelopment())
{
<link rel="stylesheet" href="~/css/sasw.css" asp-append-version="true" />
}
else
{
<!-- Google Analytics here for example -->
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
}
</head>
This solution plays nicely with the bundle and minification policy defined at bundleconfig.json
In my case the bundleconfig.json
looks like this:
[
{
"outputFileName": "wwwroot/css/site.min.css",
"inputFiles": [
"wwwroot/css/sasw.css"
]
},
{
"outputFileName": "wwwroot/js/site.min.js",
"inputFiles": [
"wwwroot/js/sasw.js"
],
"minify": {
"enabled": true,
"renameLocals": true
},
"sourceMap": false
}
]
and I have added a nuget called BuildBundlerMinifier
that minifies & bundles automatically the js and css placing the minified version in the same folder as the original one.
PS: Remember to ignore (e.g: at .gitignore
) *.min.js
and *.min.css
if you follow the same approach, to let the build process generate a new one each time without committing it to source control.