[英]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.