简体   繁体   English

在html页面上检索web.config appsettings

[英]retriving web.config appsettings on html page

I wanna use appsettings value from web.config file on html page in javascript or jquery. 我想使用javascript或jquery的html页面上的web.config文件中的appsettings值。 The content from my web.config file is: 我的web.config文件中的内容是:

<configuration>
 <appSettings> 
   <add key="ServiceURL" value="http://localhost:16917/Service1.svc"></add>
 </appSettings>
</configuration>

I have tried with 我尝试过

var serviceUrl='<%=ConfigurationManager.AppSettings["ServiceURL"].ToString() %>'
alert(serviceUrl);

Instead of showing value from webconfig it is showing <%=ConfigurationManager.AppSettings["ServiceURL"].ToString() %> on alert. 而不是显示来自webconfig的值,而是在警报上显示<%=ConfigurationManager.AppSettings["ServiceURL"].ToString() %>

Can anybody help me please. 有人可以帮我吗。

I think you are missing closing tag '>' 我认为您缺少结束标记'>'

<configuration>
 <appSettings> 
   <add key="ServiceURL" value="http://localhost:16917/Service1.svc"> </add>
 </appSettings>
</configuration>

1. Change WebConfig UI 1.更改WebConfig UI

<pages pageBaseType="Main.Web.ViewPage">

  1. Create an abstract class that implement from the WebViewPage. 创建一个从WebViewPage实现的抽象类。
 public abstract class ViewPage<T> : WebViewPage<T> { public ViewPage() { } public JSSettings UISession { get { var appSettingsFileBasedSettings = new JSSettings() { SessionTimeoutMinutes = ConfigurationManager.AppSettings["SessionTimeoutMinutes"] }; return appSettingsFileBasedSettings; } } } 
  1. In the html page just do something like. 在html页面中做类似的事情。
 <script type="text/javascript"> var show = { Settings : @Html.Raw(Json.Encode(this.UISession)) }; console.log(show); </script> 

Hope it help. 希望对您有所帮助。

This should work: 这应该工作:

var test = {
    ServiceURL: <%=ConfigurationManager.AppSettings["ServiceURL"] %>
}

You can't do that with static HTML file, at least not in any good way. 您不能使用静态HTML文件执行此操作,至少不能以任何好的方式进行。

Instead, use the power of ASP.NET and work with a Web Form, where you have Code Behind. 而是使用ASP.NET的功能,并使用其中具有代码隐藏功能的Web窗体。

In the code behind, send the value from the server to the client as JS variable: 在后面的代码中,将值作为JS变量从服务器发送到客户端:

protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "ServiceURL_JS", 
        string.Format("var serviceUrl = \"{0}\"; ", 
        ConfigurationManager.AppSettings["ServiceURL"]), true
    );
}

Now you can access the variable serviceUrl in the client side JS. 现在,您可以在客户端JS中访问变量serviceUrl

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM