简体   繁体   中英

'string' does not contain a definition for 'AsInt' in .Net Core

I am trying to introduce below code in.Net core MVC 3.1 cshtml file:

<p id="DisableInfo">
    Your session will expire in @(System.Configuration.ConfigurationManager.AppSettings["SessionExpNotice"].AsInt() / 6) minutes, Click Ok to remain logged in or click Cancel to log off.
    If you are logged off any changes will be lost.
</p>

But when in converted to .NET Core, I'm getting this error

'string' does not contain a definition for 'AsInt' and no accessible extension method 'AsInt' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

.AsInt() is an extension method for Microsoft.AspNet.WebPages.

Alternative, convert to int type via Convert.ToInt32() .

@(Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SessionExpNotice"]) / 6)

Or else you need to implement the extension method for string.AsInt() .

public static class StringExtensions
{
    public static int AsInt(this string @value)
    {   
        return Int32.TryParse(@value, out int output) 
            ? output 
            : 0;
    }
}

If you are using .NET Core,try to use:

@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<p id="DisableInfo">
        Your session will expire in @(Convert.ToInt32(Configuration["SessionExpNotice"]) / 6) minutes, Click Ok to remain logged in or click Cancel to log off.
        If you are logged off any changes will be lost.
    </p>

appsettings.json:

{
  ...
  "SessionExpNotice": 6
}

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