简体   繁体   English

Web.Config键值

[英]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. 我想知道如何在web.config文件中的vaue字段的字符串中给空格。 for example <add key="number" value="0001 " /> 例如<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. 我想在上面的值字段中的“ 1”后面输入一个空格,我尝试在值字段上直接给一个空格,但这不会发生。

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. 大家好,对不起,您不清楚。这就是我想要的,正在在web.config文件中添加以下内容”我将其用作文本框的前缀,一旦用户单击特定键,以上内容在文本框中输入前缀,允许用户仅在后几位数后缀。

So in my code 所以在我的代码中

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

But i want a space after the 0001 to also be prefixed. 但是我想在0001之后也加一个空格。 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. 因此,如果我的应用程序设置中包含了spacce,那就更好了,这样用户只需要在配置文件上而不是在代码上进行更改即可。 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. 根据D Stanley的回答,这对我有用。

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. 1)在用户可编辑的内容之前,将前缀放在用户无法编辑的单独文本框或标签中。 This should eliminate the possibility that your prefix is being typed over by the user. 这应该消除用户键入您的前缀的可能性。

2) You could format your text server side; 2)您可以格式化文本服务器端; 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. 是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: 此行为实际上是Raw XML解析的一部分,如下所述:

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: 我只是使用下面的@Kyle建议验证了以下设置,并且确实起作用了,因此Kyle建议可以用作答案:

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. 这将为您的值增加1个空间。

Are you using the standard AppSettings element or some custom settings provider? 您使用的是标准AppSettings元素还是某些自定义设置提供程序? This works for me: 这对我有用:

app.config: 的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? 从web.config中拉出字符串后,是否会修剪字符串? How are you using the setting as a prefix? 您如何将设置用作前缀?

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

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