![](/img/trans.png)
[英]Passing a Request from React.JS front-end to C# back-end (ASP.NET) using HTTP Post (RESTful API)
[英]howto retrieve asp.net appSettings key from front-end .js file using knockout.js
我有一个 asp.net 后端应用程序,我正在使用 web.config 和其他文件来存储配置密钥。
我有一个使用 knockout.js 的 javascript 文件构建的前端。
我们想知道如何从后端的 web.config 中检索键值,并使用 javascript 和 knockout.js 在前端读取该值。
有没有简单的方法来做到这一点???,视图是 javascript 文件而不是 asp.net web 页面
<script>
。
String
值呈现为 JavaScript 字符串文字时,请务必正确地进行 JS 编码(不是 HTML 编码!)。 ...否则反斜杠和引号将按字面意思呈现,这将破坏您呈现的<script>
元素,并可能引入严重的 XSS 漏洞。
在 ASP.NET 4.x 中,使用HttpUtility.JavaScriptStringEncode
确保 C#/.NET string
值正确且安全地编码为 JavaScript 字符串。
在 ASP.NET Core 中可以继续使用HttpUtility.JavaScriptStringEncode
(在现在几乎为空System.Web.dll
System.Web.HttpUtility.dll
in .NET Core 2.x or later) however now the preferred API in .NET is System.Text.Encodings.Web.JavaScriptEncoder
's Encode
method (tip: use JavaScriptEncoder.Default.Encode
).
请注意, HttpUtility.JavaScriptStringEncode
可以选择为您添加分隔引号,但System.Text.Encodings.Web.JavaScriptEncoder
从不呈现外部引号。
.cshtml
: (我假设你的<head>
在_Layout.cshtml
)
@using System.Configuration
@using System.Web.Configuration
<html>
<head>
<script>
var myAppSetting = '@( HttpUtility.JavaScriptStringEncode( WebConfigurationManager.AppSettings["myAppSetting"], addDoubleQuotes: false ) )';
</script>
</head>
<body>
</body>
.aspx
/ .ascx
/ .master
和/或 ASP.NET MVC 1.x 和 2.x:<head>
在Layout.master
)<%=
而不是<%:
直接渲染,因为我们不想对这个 JavaScript 字符串文字进行 HTML 编码。<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.Configuration" %>
<html>
<head>
<script>
var myAppSetting = '<%= HttpUtility.JavaScriptStringEncode( WebConfigurationManager.AppSettings["myAppSetting"], addDoubleQuotes: false ) %>';
</script>
</head>
<body>
</body>
.cshtml
视图:@inject IConfiguration
立即访问 .NET Core 的 appSettings。HttpUtility.JavaScriptStringEncode(...)
或System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode(...)
(包括 Z303CB0EF9EDB9082D61BBBE5825)
Html.Raw()
来呈现 JS 文字字符串。 不要使用普通的 Razor “写入”语法(即@( value )
或@value
),因为它会对您不想要的 JavaScript 进行 HTML 编码。@using System.Text.Encodings.Web
@inject IConfiguration Config
<html>
<head>
<script>
var myAppSetting = '@Html.Raw( JavaScriptEncoder.Default.Encode( this.Config.GetValue<String>("appSettings:myAppSetting") )';
</script>
</head>
<body>
</body>
Note that if you want null
values from your appSettings.json to appear as JavaScript null
literals then you need to do that manually - you can use a @functions
method to handle this, like so:
@using System.Text.Encodings.Web
@using Microsoft.AspNetCore.Html
@inject IConfiguration Config
@functions{
public HtmlString GetAppSettingJSString( String name )
{
if( name is null ) throw new ArgumentNullException(nameof(name));
String? appSettingValue = this.Config.GetValue<String?>( "appSettings:" + name );
return ToJSStringLiteral( appSettingValue );
}
private static readonly HtmlString _nullLiteral = new HtmlString( "null" );
public static HtmlString ToJSStringLiteral( String? value )
{
if( value is null ) return _nullLiteral;
String jsStringLiteralChars = JavaScriptEncoder.Default.Encode( value );
return new HtmlString( '"' + jsStringLiteralChars + '"' );
}
}
<html>
<head>
<script>
var myAppSetting = @Html.Raw( this.GetAppSettingJSString( "myAppSetting" ) );
</script>
</head>
<body>
</body>
所以现在上面的<script>
将呈现为:
<script>
var myAppSetting = "foobarbaz";
</script>
...或这个:
<script>
var myAppSetting = null;
</script>
任何脚本(除了 ES 模块,我认为?)都可以通过隐式global
(又名window
)object 访问myAppSetting
,因为所有顶级var
声明都成为全局属性。
就像这样:
<script>
document.addEventListener( 'DOMContentLoaded', function() {
alert( "myAppSetting: \"" + window.myAppSetting + "\"" );
} );
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.