用于VB.NET的等效C#“会话” [英] equivalent C# for VB.NET "Session"

查看:106
本文介绍了用于VB.NET的等效C#“会话”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨组,


任何人都可以给我这个VB.ET代码的等效C#锐代码,


:: VB。 NET ::


Private Sub Page_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)_

处理MyBase.Load

''当

保护

''页面加载时,loginCheck用户控件中的此事件例程将触发。如果用户尚未登录,则loginCheck会将

用户重定向到LoginPageURL指定的页面以进行验证。

''如果用户已登录



''他或她可以访问受保护的页面。


如果未启用然后退出Sub

If IsNothing(Session(LoginType))然后退出Sub

如果是CType(Session(LoginType),_

String).Length> 0然后

如果IsNothing(会话(LoginPageURL& _

" _Valid"))那么

会话(" LoginTarget")= _

Request.ServerVariables _

(" Script_Name")

Response.Redirect(LoginPageURL)

Else

如果CType(会话(LoginPageURL& _

" _Valid"),String).Length = 0则

会话(" LoginTarget")= _

Request.ServerVariables _

(" Script_Name")

Response.Redirect(LoginPageURL)

结束如果

结束如果

结束如果

结束子


C#?


我使用了一个自动工具来做到这一点并得到:


MS VS 2003总是抱怨会话如下所示:


c:\inetpub \wwwroot\passwordProtectCSharp\Global.as ax.cs(42):

''System.Web .HttpApplication.Session''表示''属性'',其中''方法''

预计


任何人都有任何建议?


谢谢。 -Dale

Hi groups,

Can anyone give me the equivalent C# sharp code for this VB.ET code,

:: VB.NET ::

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles MyBase.Load
''This event routine in the loginCheck user control fires when the
protected
''page loads. If the user hasn''t logged in, loginCheck redirects the
user to
''the page specified by LoginPageURL for validation. If the user has
logged in,
''he or she gets access to the protected page.

If Not Enabled Then Exit Sub
If IsNothing(Session("LoginType")) Then Exit Sub
If CType(Session("LoginType"), _
String).Length > 0 Then
If IsNothing(Session(LoginPageURL & _
"_Valid")) Then
Session("LoginTarget") = _
Request.ServerVariables _
("Script_Name")
Response.Redirect(LoginPageURL)
Else
If CType(Session(LoginPageURL & _
"_Valid"), String).Length = 0 Then
Session("LoginTarget") = _
Request.ServerVariables _
("Script_Name")
Response.Redirect(LoginPageURL)
End If
End If
End If
End Sub

C#?

I used an auto tool to do this and got:

The MS VS 2003 always complains "Session" as below:

c:\inetpub\wwwroot\passwordProtectCSharp\Global.as ax.cs(42):
''System.Web.HttpApplication.Session'' denotes a ''property'' where a ''method''
was expected

Anyone has any suggestions?

Thanks. -Dale

推荐答案

<" =?Utf-8?B?ZGFsZSB6aGFuZw ==?=" < dale
zh***@discussions.microsoft.com >>写道:


< snip>
<"=?Utf-8?B?ZGFsZSB6aGFuZw==?=" <dale
zh***@discussions.microsoft.com>> wrote:

<snip>
我使用了一个自动工具来做到这一点并得到:

MS VS 2003总是抱怨会话。如下:

c:\inetpub \wwwroot\passwordProtectCSharp\Global.as ax.cs(42):
''System.Web.HttpApplication.Session''表示a''property''预计''方法'


任何人都有任何建议?
I used an auto tool to do this and got:

The MS VS 2003 always complains "Session" as below:

c:\inetpub\wwwroot\passwordProtectCSharp\Global.as ax.cs(42):
''System.Web.HttpApplication.Session'' denotes a ''property'' where a ''method''
was expected

Anyone has any suggestions?




这表明您正在使用它作为:


会话(LoginType),当您应该使用Session [" LoginType"] -

ie你应该使用Session属性的索引器。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



That suggests that you''re using it as:

Session("LoginType") when you should be using Session["LoginType"] -
i.e. you should be using the indexer of the Session property.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


谢谢,Jon。解决了这个问题。


但是有一些新问题:

1. VB :: Response.Redirect _

(会话(LoginTarget))

C#::?

Response.Redirect(Session [" LoginTarget"]);

c:\inetpub \wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(84):'System.Web.HttpResponse.Redirect(string)的最佳

重载方法匹配''已经

一些无效的论点

c:\inetpub \wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(84):参数

''1'':无法从''对象''转换为''字符串''


2.VB :: Dim sPage As String = Session(" LoginTarget" ;)

C#:: string sPage = Session [" LoginTarget"];

c:\inetpub \wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs (108):不能

隐式转换类型''对象'' ''string''


3.VB ::

私人功能CheckPass(ByVal用户为字符串,ByVal密码为

String)As Boolean

Dim dbConn As OleDbConnection

Dim dbCmd As OleDbCommand

Dim dbDR As OleDbDataReader

Dim sConn As String = Connect()

Dim sSQL As String

CheckPass = False

C#::

private bool CheckPass(字符串User,string Password)

{

OleDbConnection dbConn;

OleDbCommand dbCmd;

OleDbDataReader dbDR ;

string sConn = Connect();

string sSQL;

CheckPass = false; ????

c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(131):方法

''passwordProtectCSharp.loginControl.CheckPass( stri ng,string)''引用

没有括号


4. VB :: Date.Now.ToString

c#? ?


5. VB :: If(datData.ToString).Length然后

C#:: if(datData.ToString.Length)

c:\inetpub \wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(272):

''System.DateTime.ToString(string)''表示''方法''在

中无效给定的上下文


非常感谢。 -Dale

" Jon Skeet [C#MVP]"写道:
Thanks, Jon. That problem was sloved.

But a few new ones:
1. VB:: Response.Redirect _
(Session("LoginTarget"))

C#::?
Response.Redirect(Session["LoginTarget"]);
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(84): The best
overloaded method match for ''System.Web.HttpResponse.Redirect(string)'' has
some invalid arguments
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(84): Argument
''1'': cannot convert from ''object'' to ''string''

2.VB:: Dim sPage As String = Session("LoginTarget")
C#:: string sPage = Session["LoginTarget"];
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(108): Cannot
implicitly convert type ''object'' to ''string''

3.VB::
Private Function CheckPass(ByVal User As String, ByVal Password As
String) As Boolean
Dim dbConn As OleDbConnection
Dim dbCmd As OleDbCommand
Dim dbDR As OleDbDataReader
Dim sConn As String = Connect()
Dim sSQL As String
CheckPass = False
C#::
private bool CheckPass(string User, string Password)
{
OleDbConnection dbConn;
OleDbCommand dbCmd;
OleDbDataReader dbDR;
string sConn = Connect();
string sSQL;
CheckPass = false; ???
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(131): Method
''passwordProtectCSharp.loginControl.CheckPass(stri ng, string)'' referenced
without parentheses

4. VB:: Date.Now.ToString
c#??

5. VB:: If (datData.ToString).Length Then
C#:: if (datData.ToString.Length)
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(272):
''System.DateTime.ToString(string)'' denotes a ''method'' which is not valid in
the given context

Many thanks. -Dale
"Jon Skeet [C# MVP]" wrote:
<" =?Utf-8?B?ZGFsZSB6aGFuZw ==?=" < dale
zh***@discussions.microsoft.com >>写道:

< snip>
<"=?Utf-8?B?ZGFsZSB6aGFuZw==?=" <dale
zh***@discussions.microsoft.com>> wrote:

<snip>
我使用了一个自动工具来做到这一点并得到:

MS VS 2003总是投诉会话如下:

c:\inetpub \wwwroot\passwordProtectCSharp\Global.as ax.cs(42):
''System.Web.HttpApplication.Session''表示一个''属性''预计''方法'


任何人都有任何建议?
I used an auto tool to do this and got:

The MS VS 2003 always complains "Session" as below:

c:\inetpub\wwwroot\passwordProtectCSharp\Global.as ax.cs(42):
''System.Web.HttpApplication.Session'' denotes a ''property'' where a ''method''
was expected

Anyone has any suggestions?



这表明你''当你应该使用Session [" LoginType"]时,使用它作为:

会话(LoginType) -
即你应该使用Session属性的索引器。 br />
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



That suggests that you''re using it as:

Session("LoginType") when you should be using Session["LoginType"] -
i.e. you should be using the indexer of the Session property.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



dale zhang< da ******* @ discussion.microsoft.com>写道:
dale zhang <da*******@discussions.microsoft.com> wrote:
谢谢,乔恩。这个问题已经解决了。

但是有一些新问题:
1. VB :: Response.Redirect _
(会话(" LoginTarget"))

C#::?
Response.Redirect(Session [" LoginTarget"]);
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(84) :''System.Web.HttpResponse.Redirect(string)''的最佳
重载方法匹配有一些无效的参数
c:\inetpub\wwwroot\passwordProtectCSharp\\ loginCont rol.ascx.cs(84):参数
''1':无法从''对象''转换为''字符串''


你'' ll需要转换为字符串:


Response.Redirect((string)Session [" LoginTarget"]);


事实是你没有在VB中表明你有Option Strict

关闭 - 一个坏主意!

2.VB :: Dim sPage As String = Session(" LoginTarget")
C#:: string sPage = Session [" LoginTarget"];
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(108):无法隐式转换类型''对象' 'to''string''


再次相同。

3.VB ::
私人函数CheckPass(ByVal User As String,ByVal密码as
String)As Boolean
Dim dbConn As OleDbConnection
Dim dbCmd As OleDbCommand
Dim dbDR As OleDbDataReader
Dim sConn As String = Connect()
Dim sSQL As String
CheckPass = False
C#::
私有bool CheckPass(字符串User,string Password)
{OleDbConnection dbConn;
OleDbCommand dbCmd;
OleDbDataReader dbDR;
字符串sConn = Connect();
字符串sSQL;
CheckPass = false; ????
c:\inetpub \wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(131):方法
''passwordProtectCSharp.loginControl.CheckPass(s​​tri ng,string)''引用
没有括号


不确定 - 什么是CheckPass = false准备好了吗?

有一个名为CheckPass的变量吗?不要忘记C#是区分大小写的b $ b,而VB.NET不是。

4. VB :: Date.Now.ToString c#??


DateTime.Now.ToString()

5. VB :: If(datData.ToString).Length然后
C#:: if( datData.ToString.Length)
c:\inetpub \wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(272):
''System.DateTime.ToString(string)''表示一个''方法''在给定的上下文中无效
Thanks, Jon. That problem was sloved.

But a few new ones:
1. VB:: Response.Redirect _
(Session("LoginTarget"))

C#::?
Response.Redirect(Session["LoginTarget"]);
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(84): The best
overloaded method match for ''System.Web.HttpResponse.Redirect(string)'' has
some invalid arguments
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(84): Argument
''1'': cannot convert from ''object'' to ''string''
You''ll need to cast to string:

Response.Redirect ((string) Session["LoginTarget"]);

The fact that you didn''t in VB suggests that you had Option Strict
turned off - a bad idea!
2.VB:: Dim sPage As String = Session("LoginTarget")
C#:: string sPage = Session["LoginTarget"];
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(108): Cannot
implicitly convert type ''object'' to ''string''
Same again.
3.VB::
Private Function CheckPass(ByVal User As String, ByVal Password As
String) As Boolean
Dim dbConn As OleDbConnection
Dim dbCmd As OleDbCommand
Dim dbDR As OleDbDataReader
Dim sConn As String = Connect()
Dim sSQL As String
CheckPass = False
C#::
private bool CheckPass(string User, string Password)
{
OleDbConnection dbConn;
OleDbCommand dbCmd;
OleDbDataReader dbDR;
string sConn = Connect();
string sSQL;
CheckPass = false; ???
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(131): Method
''passwordProtectCSharp.loginControl.CheckPass(stri ng, string)'' referenced
without parentheses
Not sure on that - what is "CheckPass = false" meant to do, exactly? Is
there a variable called CheckPass somewhere? Don''t forget that C# is
case-sensitive, whereas VB.NET isn''t.
4. VB:: Date.Now.ToString
c#??
DateTime.Now.ToString()
5. VB:: If (datData.ToString).Length Then
C#:: if (datData.ToString.Length)
c:\inetpub\wwwroot\passwordProtectCSharp\loginCont rol.ascx.cs(272):
''System.DateTime.ToString(string)'' denotes a ''method'' which is not valid in
the given context




使用ToString()。长度而不是ToString.Length。当没有任何参数时,VB.NET不会使用括号,不幸的是 -

使方法调用看起来像属性:(


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件太



Use ToString().Length rather than ToString.Length. VB.NET doesn''t make
you use brackets when there aren''t any parameters, unfortunately -
making method calls look like properties :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于用于VB.NET的等效C#“会话”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆