简体   繁体   English

如何将配置值从Asp.Net Core MVC 2.0配置文件推送到React TypeScript客户端脚本?

[英]How to push configuration values from Asp.Net Core MVC 2.0 config file to React TypeScript client script?

I have an application on ASP.NET MVC Core 2.0 using React Template . 我使用React TemplateASP.NET MVC Core 2.0上有一个应用程序。 I have propertie in config file on server side - SiteURL . 我在服务器端的配置文件中有属性 - SiteURL I need to push it to client TypeScript , and don`t understand how can I make it? 我需要将它推送到客户端TypeScript ,并且不知道我该如何制作它?

I tried to push them throw Home/Index file by placing in the last script 我试图通过放入最后一个脚本来推送它们抛出Home/Index文件

SiteProps.SiteURL= "http://mysiteurl.org"

and on client I make 在我做的客户

export var SiteProps: { SiteURL: string } = {SiteURL: ""};

and import this file on boot 并在启动时导入此文件

But testing this property on console I see 但是我在控制台上测试这个属性

Uncaught ReferenceError: SiteProps is not defined 未捕获的ReferenceError:未定义SiteProps

I think that JavaScript did't see my propertie from TypeScript context. 我认为JavaScriptTypeScript上下文中看到我的属性。

The question is how to make this code work, and what is the best practice to push const values (properties) to client TypeScript code. 问题是如何使这个代码工作,以及将const值(属性)推送到客户端TypeScript代码的最佳实践是什么。

I found solution to make this line on Home/Index : 我在Home/Index上找到了解决方案:

<div id="site-props" style="display: none;">@ViewBag.PropsJSON</div>

Where PropsJSON is JSON string of my configuration. 其中PropsJSON是我配置的JSON字符串。 After it i initialize my clientscript configuration using this code: 之后,我使用以下代码初始化我的客户端配置:

function ImportProps() {
    var ell = document.getElementById('site-props');
    var jsontext = ell!.innerText;
    SiteProps = JSON.parse(jsontext);
}

Some more ways how to solve it. 更多如何解决它的方法。

One. 一。

When application starts to rewrite file "mysetting.js" with actual settings and to ref this file in index.html 当应用程序开始使用实际设置重写文件“mysetting.js”并在index.html引用此文件时

mysetting.js mysetting.js

var settings = {
  SiteName: "MySite",
  SiteApi: "http://site.api"
}

index.html 的index.html

<script src="mysetting.js"></script>

Two. 二。

In Home/Index set window.settings in script block Home/Index在脚本块中设置window.settings

window.settings = {
  SiteName: "MySite",
  SiteApi: "http://site.api"
}

and it should be available from all code. 它应该可以从所有代码中获得。

Three. 三。

After client application starts get all settings with fetch request from server on the fly, and get result as JSON object, and set it somewhere to static. 客户端应用程序启动后,即时获取来自服务器的获取请求的所有设置,并将结果作为JSON对象,并将其设置为静态。

fetch("api/Settings/getSettings").
   then( /* set data to static */ )

Where you initialize your client code. 初始化客户端代码的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何阻止ASP.NET Core 2.0 MVC缩小修改特定文件? - How can I stop ASP.NET Core 2.0 MVC minification from modifying a specific file? ASP.Net Core:将配置值传递给 JavaScript 客户端代码 - ASP.Net Core: passing configuration values to JavaScript client code ASP.NET Core 2.0 MVC 6.如何管理每个视图的javascript文件? - ASP.NET Core 2.0 MVC 6. How to manage javascript file for each view? Asp.net MVC-从服务器推送延迟的客户端消息 - Asp.net MVC - Push a deferred client message from server 如何在asp.net mvc中进行客户端验证后运行脚本 - How to run a script after client validation in asp.net mvc 从客户端(jQuery)检查ASP.NET MVC值(角色) - Check ASP.NET MVC values (roles) from client (jQuery) 如何使用ASP.NET MVC4 Razor项目中的web.config文件中的值更新JavaScript? - How to update JavaScript with a value from web.config file in an ASP.NET MVC4 Razor project? 如何在ASP.NET CORE MVC中导入一个js文件模块? - How to import a js file module in ASP.NET CORE MVC? 如何从ASP.Net中的用户控件调用客户端脚本? - How to call client script from User Control in ASP.Net? 如何对ASP.NET Core 2.0剃须刀页面中按下的按钮做出反应 - how to react to button pushed in ASP.NET Core 2.0 razor pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM