问题
Can i read data from web.config using JQuery ?
回答1:
Jquery is javascript that runs in your browser, your web.config resides on your server. If you want to expose data from your web.config, I think you should create some kind of webservice that you can call from the client side javascript to get the data you want.
If you would be able to directly read from the web.config file, then it would be a major security risk as the web.config file is often used for storing sensitive information like connection strings etc.
回答2:
The best solution when using none aspx/HTML5 :
1.Create file "Web.config.js"
(java script file) in the project root
2.Add ref to the JS in your HTML file
<script src="Web.config.js" type="text/javascript"></script>
3.add the key & val to the "Web.config.js"
:
var prmKEY = "myVal"
4.Access prmKEY from JQuery as global parameter
回答3:
You can also store the data as a cookie in the OnPreRender(EventArgs e)
or Page_Load(object sender, EventArgs e)
(if your using the Page_Load()
store the cookie in the if (!IsPostBack){}
so your not storing it multiple times) and read it on the client side to use for whatever purpose. I usually store data such as this as session storage on the client side 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");
回答4:
No, you cannot.
回答5:
I found solution without making any web service :
1- building empty aspx page that in it's load read the data from web.config and write it in the page using Response.Write(**)
2- using JQuery to read the result from created page as follow :
$.get
(
"JQueryPage.aspx",
function(result) {
// .. set variable to result and use it
}
};
回答6:
- In Page Load event store the config value in hidden fields using Config Manager.
- Retire from hidden fields using J query.
回答7:
You can create a hidden textbox with variable on html and assign value from config file to it. Assign a id to the hidden control and grab details of that using jquery.
回答8:
I'd assume you could read the value from your webconfig in your controller. Inject that value into your view and then use jQuery to retrieve that value. That would be the way I'd approach it. I'd place it in my appsettings element. Shown here how to get from your webconfig. https://msdn.microsoft.com/en-us/library/610xe886.aspx , then I would inject that into my view using Viewbag.
来源:https://stackoverflow.com/questions/2801713/can-i-read-data-from-web-config-using-jquery