如何将System.Web.Configuration.WebConfigurationManager.AppSettings从String转换为INT

时间:2022-08-10 03:44:10

I have defined the following inside my web.config file:-

我在web.config文件中定义了以下内容: -

<add key="TechPageSize" value="20" />

But I m unable to reference this value inside my paging parameters as follow:-

但我无法在我的分页参数中引用此值,如下所示: -

var servers = repository.AllFindServers(withOutSpace).OrderBy(a => a.Technology.Tag).ToPagedList(page, (Int32)System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"]);

and I will get an error that it can not change String to INT.

我将得到一个错误,它不能将String更改为INT。

Any idea what is the problem ?

不知道是什么问题?

3 个解决方案

#1


13  

int techPageSize;
if (!int.TryParse(ConfigurationManager.AppSettings["TechPageSize"], out techPageSize))
{
    throw new InvalidOperationException("Invalid TechPageSize in web.config");
}

Int32.TryParse has two effects:

Int32.TryParse有两个效果:

  • It converts the app setting to an integer and stores the result in techPageSize, if possible.
  • 如果可能,它会将应用程序设置转换为整数并将结果存储在techPageSize中。
  • If the value cannot be converted, the method returns False, allowing you to handle the error as you see fit.
  • 如果无法转换该值,则该方法返回False,允许您根据需要处理错误。

PS: It suffices to use ConfigurationManager.AppSettings, once you have imported the System.Configuration namespace.

PS:导入System.Configuration命名空间后,只需使用ConfigurationManager.AppSettings即可。

#2


3  

The result of

的结果

System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"]

is a string, you can't just cast a string to int. Use Convert.ToInt32 or int.TryParse to get the integer value of the string.

是一个字符串,你不能只是将一个字符串强制转换为int。使用Convert.ToInt32或int.TryParse获取字符串的整数值。

int pagesize = Convert.ToInt32(System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"]);

or

要么

bool succeed = int.TryParse(System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"], out pagesize);

#3


2  

Try:

尝试:

Convert.ToInt32(System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"])

This will error if the convert fails. You could use int.Tryparse in case it doesn't convert to an int, but the fact is this is a config setting and only you have control over this value - it's not user-submitted so catering for error would be overkill IMO.

如果转换失败,这将出错。你可以使用int.Tryparse,如果它没有转换为int,但事实是这是一个配置设置,只有你可以控制这个值 - 它不是用户提交的,所以迎合错误将是矫枉过正的IMO。

#1


13  

int techPageSize;
if (!int.TryParse(ConfigurationManager.AppSettings["TechPageSize"], out techPageSize))
{
    throw new InvalidOperationException("Invalid TechPageSize in web.config");
}

Int32.TryParse has two effects:

Int32.TryParse有两个效果:

  • It converts the app setting to an integer and stores the result in techPageSize, if possible.
  • 如果可能,它会将应用程序设置转换为整数并将结果存储在techPageSize中。
  • If the value cannot be converted, the method returns False, allowing you to handle the error as you see fit.
  • 如果无法转换该值,则该方法返回False,允许您根据需要处理错误。

PS: It suffices to use ConfigurationManager.AppSettings, once you have imported the System.Configuration namespace.

PS:导入System.Configuration命名空间后,只需使用ConfigurationManager.AppSettings即可。

#2


3  

The result of

的结果

System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"]

is a string, you can't just cast a string to int. Use Convert.ToInt32 or int.TryParse to get the integer value of the string.

是一个字符串,你不能只是将一个字符串强制转换为int。使用Convert.ToInt32或int.TryParse获取字符串的整数值。

int pagesize = Convert.ToInt32(System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"]);

or

要么

bool succeed = int.TryParse(System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"], out pagesize);

#3


2  

Try:

尝试:

Convert.ToInt32(System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"])

This will error if the convert fails. You could use int.Tryparse in case it doesn't convert to an int, but the fact is this is a config setting and only you have control over this value - it's not user-submitted so catering for error would be overkill IMO.

如果转换失败,这将出错。你可以使用int.Tryparse,如果它没有转换为int,但事实是这是一个配置设置,只有你可以控制这个值 - 它不是用户提交的,所以迎合错误将是矫枉过正的IMO。