简体   繁体   English

如何在ASP.NET vs2015中使用C#将Context.User.Identity.GetUserId()转换为Int

[英]How do i convert Context.User.Identity.GetUserId() to int with c# in asp.net vs2015

How do I convert Context.User.Identity.GetUserId() to an int. 如何将Context.User.Identity.GetUserId()转换为int。

code: 码:

string clientId = Context.User.Identity.GetUserId();
if (clientId != null) {
    cart NewInCart = new cart();
    NewInCart.ClientId = Convert.ToInt32(clientId);
}

Above code gives error stating: 上面的代码给出了错误说明:

when converting a string to DateTime, parse the string to take the date before putting each variable into the DateTime object. 将字符串转换为DateTime时,在将每个变量放入DateTime对象之前,分析字符串以获取日期。

我认为您想要的是:

NewInCart.ClientId = Int32.Parse(clientId);

Sorted for now. 现在排序。

I have decided to change the data type of ClientID in the dbo to nvarchar(50); 我决定将dbo中ClientID的数据类型更改为nvarchar(50); that way I save like this: 这样,我这样保存:

string clientId = Context.User.Identity.GetUserId() ;
.......
cart.ClientId = clientId;

where cart.ClientId is now expecting a string. 其中cart.ClientId现在需要一个字符串。 This sorted my problem and I no longer need to convert Context.User.Identity.GetUserId() to int. 这解决了我的问题,我不再需要将Context.User.Identity.GetUserId()转换为int。

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

相关问题 如何在安装vs2015之后在vs2015上安装Asp.Net? - How can I install Asp.Net on vs2015 after already installing vs2015 ? 更改Asp.net身份和GetUserID的主键<int> - Change Primary Key for Asp.net Identity and GetUserID<int> 为不同的目标文件夹VS2015构建C#ASP.NET解决方案 - Build C# ASP.NET solution to different target folder VS2015 如果网站创建为 Visual C# ASP.Net Web 应用程序在 VS2015 中,PageMethods 不起作用 - PageMethods not working if the website is created as a Visual C# ASP.Net Web Application in VS2015 User.Identity.GetUserId()在ASP.NET MVC5中的工作方式 - How User.Identity.GetUserId() work in ASP.NET MVC5 VS2015 C#,如何让登录表单引用用户名和密码进行身份验证 - VS2015 C#, How do I get the login form to reference usernames and passwords for authentication C#-如何在VS2015中构建/调试此代码? - C# - How do I build/debug this code in VS2015? 我如何模拟User.Identity.GetUserId()? - How do I mock User.Identity.GetUserId()? 如何在C#ASP.Net中转换为int AngularJS元素? - How to convert to int AngularJS element in C# ASP.Net? CS0029 C# 无法将类型隐式转换为“int?” ASP.NET CORE 5 MVC 标识 - CS0029 C# Cannot implicitly convert type to 'int?' ASP.NET CORE 5 MVC Identity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM