本文介绍了根据Web.config appSettings设置API URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我继承了一个有角度的应用程序,该应用程序将API url定义为一个常量变量,并且可以正常工作.但是,这样做有其缺点,因为当我要指向部署API,测试API和开发API时,必须更改变量值.

So I've an angular application that I've inherited that defines the API url as a constant variable and it works. However this has it's drawbacks as I have to change the variable value when I want to point at the deployment API, testing API and development API.

是否可以访问我的web.config文件以读取appSettings并返回API URL? (我使用XML转换应用了不同的API URL).

Is there a way to access my web.config file to read the appSettings and return the API URL? (I've applied different API URL'S using XML transformations).

我已经实现了一个解决方案,该解决方案可以从我的Web项目访问API控制器并以这种方式返回值,但是我不确定这是否是正确的方法.

I have implemented a solution that accesses an API controller from my web project and returns the value that way but I'm not sure if this is the correct way of doing it.

推荐答案

您可以将值从web.config序列化为JSON,并将其添加到JavaScript可以读取和解析的<script type="application/json">元素中.

You can serialize values from web.config to JSON and add them to a <script type="application/json"> element which your JavaScript can read and parse.

这是我能想到的最精简的例子.您可能想要更好地封装从服务器代码编写代码的方式以及从JavaScript读取代码的方式(并提供更好的名称).

Here's the most pared-down example I can come up with. You'd probably want to better encapsulate both how you write this from your server code and how you read it from your JavaScript (and come up with better names.)

首先,在您的web.config中,定义将包含要传递给JavaScript的键/值对的部分:

First, in your web.config, define the section which will contain the key/value pairs you want to pass to your JavaScript:

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="jsConfigurationValues"
          type="System.Configuration.NameValueSectionHandler" />
      </configSections>

      <jsConfigurationValues>
        <add key="someValue" value="a" />
        <add key="apiUrl" value="http://theurl" />
      </jsConfigurationValues>

      <!-- the rest of your web.config -->

    </configuration>

此功能在Web表单的代码背后.

In the code-behind of a web form is this function.

protected string JavascriptConfigurationValues()
{
    var configurationValues =
        (NameValueCollection)ConfigurationManager.GetSection("jsConfigurationValues");
    var dictionary = new Dictionary<string, string>();
    foreach(var key in configurationValues.AllKeys)
        dictionary.Add(key, configurationValues[key]);
    return JsonConvert.SerializeObject(dictionary);
}

在Web表单的标记中是这样的:

In the markup of the web form is this:

<script type="application/json" id="mySettings">
    <% =JavascriptConfigurationValues() %>
</script>

(您可以将其中的一些或全部放入UserControl或以其他方式封装.)

(You could put some or all of this in a UserControl or encapsulate it some other way.)

此脚本读取JSON并将其分配给变量:

This script reads the JSON and assigns it to a variable:

var mySettings = JSON.parse(document.getElementById("mySettings").textContent);

现在可以访问了:

var apiUrl = mySettings.apiUrl;

这篇关于根据Web.config appSettings设置API URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:08