Public 带有缩小 JS 和 CSS 的条件 Razor 视图
Public Conditional Razor Views with minified JS and CSS
我正在使用 ASP.NET MVC 5 开发我的网站。我正在使用 Grunt 来缩小我的 CSS 和 JS。一切正常,但如果我正在发布(发布模式)或正在调试(我需要清楚地阅读我的 CSS 和 JS),我需要更改文件的路由源。
像这样:
[Debug Mode]
<script src="scripts/myscript.js"></script>
[Relase Mode or Public Mode]
<script src="dist/myscript.min.js"></script>
我已经在 Whosebug(Razor view engine, how to enter preprocessor(#if debug)) 上阅读了这个 post 我不喜欢提议的解决方案 因为我认为 (不确定)加载的 Razon View 将始终在生产服务器中检查它是否处于发布或调试模式,我认为没有必要。
有人可以确认我是否正确吗?我需要手动进行更改吗?还有其他解决方案吗?
谢谢!!问候!!
完成此操作的一种方法是仅检查 http 上下文中的调试模式。
@if (HttpContext.Current.IsDebuggingEnabled)
{
<script src="scripts/myscript.js"></script>
}
else
{
<script src="dist/myscript.min.js"></script>
}
更新的答案
感谢 Shoe 我了解了环境标签助手的存在,所以我们现在可以像这样使用它,而不是我手动实现相同的旧答案:
<!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>
旧答案
还应与 Razor 视图(dotnet 核心 3.1+)一起使用的 Razor 页面的解决方案可能是注入 IWebHostEnvironment
并根据当前环境(由全局变量 ASPNETCORE_ENVIRONMENT
定义)有条件地呈现
在你的 _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>
此解决方案与 bundleconfig.json
中定义的 bundle and minification 策略配合得很好
在我的例子中,bundleconfig.json
看起来像这样:
[
{
"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
}
]
我添加了一个名为 BuildBundlerMinifier
的 nuget,它会自动缩小和捆绑 js,css 将缩小版本放在与原始版本相同的文件夹中。
PS:记得忽略(例如:在.gitignore
)*.min.js
和*.min.css
如果你遵循相同的方法,让构建过程生成一个新的每次一个,而无需将其提交给源代码管理。
我正在使用 ASP.NET MVC 5 开发我的网站。我正在使用 Grunt 来缩小我的 CSS 和 JS。一切正常,但如果我正在发布(发布模式)或正在调试(我需要清楚地阅读我的 CSS 和 JS),我需要更改文件的路由源。
像这样:
[Debug Mode]
<script src="scripts/myscript.js"></script>
[Relase Mode or Public Mode]
<script src="dist/myscript.min.js"></script>
我已经在 Whosebug(Razor view engine, how to enter preprocessor(#if debug)) 上阅读了这个 post 我不喜欢提议的解决方案 因为我认为 (不确定)加载的 Razon View 将始终在生产服务器中检查它是否处于发布或调试模式,我认为没有必要。
有人可以确认我是否正确吗?我需要手动进行更改吗?还有其他解决方案吗?
谢谢!!问候!!
完成此操作的一种方法是仅检查 http 上下文中的调试模式。
@if (HttpContext.Current.IsDebuggingEnabled)
{
<script src="scripts/myscript.js"></script>
}
else
{
<script src="dist/myscript.min.js"></script>
}
更新的答案
感谢 Shoe 我了解了环境标签助手的存在,所以我们现在可以像这样使用它,而不是我手动实现相同的旧答案:
<!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>
旧答案
还应与 Razor 视图(dotnet 核心 3.1+)一起使用的 Razor 页面的解决方案可能是注入 IWebHostEnvironment
并根据当前环境(由全局变量 ASPNETCORE_ENVIRONMENT
定义)有条件地呈现
在你的 _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>
此解决方案与 bundleconfig.json
在我的例子中,bundleconfig.json
看起来像这样:
[
{
"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
}
]
我添加了一个名为 BuildBundlerMinifier
的 nuget,它会自动缩小和捆绑 js,css 将缩小版本放在与原始版本相同的文件夹中。
PS:记得忽略(例如:在.gitignore
)*.min.js
和*.min.css
如果你遵循相同的方法,让构建过程生成一个新的每次一个,而无需将其提交给源代码管理。