Can I read data from web.config using JQuery?
Jquery is javascript that runs in your browser, your web.config is on your server. If you want to output data from your web.config, I think you should create some kind of web service that you can call from client side javascript to get the data you want.
If you can read from the web.config file directly, it would be a serious security risk, since the web.config file is often used to store sensitive information like connection strings, etc.
a source to share
Better solution when using none aspx / HTML5:
1.Create a file "Web.config.js"
(java script file) in the root of the project
2. Add ref to JS to your HTML file
<script src="Web.config.js" type="text/javascript"></script>
3. Attach the key and the value val to "Web.config.js"
:
var prmKEY = "myVal"
4.Access prmKEY from JQuery as global parameter
a source to share
You can also save the data as a cookie in OnPreRender(EventArgs e)
or Page_Load(object sender, EventArgs e)
(if you are using Page_Load()
, save the cookie in if (!IsPostBack){}
so you don't store it multiple times) and read it to the client side for any purpose. I usually store data such as client side session storage to use while the program is running.
Server side:
// **This works best if the property isn't a data structure, otherwise you will need to do**
// some data manipulation to get it to work right
Response.Cookies["FOO"].Value = MyApp.Properties.Settings.Default.FOO.ToString();
Client side:
sessionStorage.FOO = readCookie("FOO");
a source to share
I found a solution without any web services:
1- create a blank aspx page that loads data from web.config and writes it to the page using Response.Write (**)
2- using JQuery to read the result from the generated page as follows way:
$.get
(
"JQueryPage.aspx",
function(result) {
// .. set variable to result and use it
}
};
a source to share
I would suggest that you can read the value from your webconfig in your controller. Inject this value into your view and then use jQuery to retrieve that value. That would be the way I approach it. I would place it in the appsettings element. Shown here how to get from your webconfig. https://msdn.microsoft.com/en-us/library/610xe886.aspx then I would inject this into my opinion using Viewbag.
a source to share