简体   繁体   中英

Web.Config Key value

I would like to know how to give a space in the string for the vaue field in web.config file. for example <add key="number" value="0001 " />

I would like to have a space entered after '1' in the above value field,I tried giving a space directly on to the value field,but this does not occur.

Kindly share your opinion/replies on the same.

Hi all, Sorry for not being clear.. This is what i want, am adding the following in the web.config file '' I use this as a prefix in the text box where once a user clicks on a particular key, the above prefix is entered in the text box allowing the user to suffix the net few digits only.

So in my code

Tbx.text=app.DeploymentConfigurations["number"];

But i want a space after the 0001 to also be prefixed. How do i give that in the key value.


Note: i do not want the space to be hard coded.. I have already done that. Space is a requirement now, but it might change later. So it would be better if i have the spacce included in my app settings so that user will have to change it only on the config file and not on the code. Thank you all

Without seeing more code, it's hard to tell what's going on; as per D Stanley's answer, this worked for me.

However, since you are dealing with user input, it's probably best to enforce the prefix server side anyway; a few options would include:

1) Putting the prefix in a separate textbox or label that the user can't edit in front of the user editable content. This should eliminate the possibility that your prefix is being typed over by the user.

2) You could format your text server side; there's no need to hard code it either, simple:

<add key="Prefix" value="0001" />
<add key="Separator" value=" " />

You can then do something like:

string overall = String.Format("{0}{1}{2}", ConfigurationManager.AppSettings["Prefix"], ConfigurationManager.AppSettings["Separator"], userInputtedText);

The user can then of course remove the space or change it to a different character or characters independently of anything else.

Why not just add the space in code?

Tbx.text=app.DeploymentConfigurations["number"] + " ";

Since your requirements require you to have the space in the config, try this:

<add key="number" value="0001&#x20;" />

&#x20; is the space character in unicode.

Barring that, use a magic string:

<add key="number" value="0001{_}" />

Then replace in code:

Tbx.text=app.DeploymentConfigurations["number"].Replace("{_}", " ");

This will allow you to control the placement of the space in the config, while still achieving the results you are looking for.

This behavior is actually part of Raw XML parsing as discussed here:

I just verified the the settings as below using @Kyle suggestion as below and it did work so Kyle suggestion can be used as Answer:

In your code, I think the best way to do is as below:

string s = app.DeploymentConfigurations["number"];
Tbx.text= s.PadRight(s.Length + 1);

This will add 1 space to your value.

Are you using the standard AppSettings element or some custom settings provider? This works for me:

app.config:

<appSettings>
  <add key="Test" value="123456 "/>
</appSettings>

code:

string setting = ConfigurationManager.AppSettings["Test"];
Debug.Assert(setting.Length==7);  // returns true

Is something trimming the string after it's pulled from web.config? How are you using the setting as a prefix?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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