当前上下文中不存在名称为'HttpContext'的内容。

12

我正在尝试将一些vb.net代码转换为C#,但是我一直在遇到错误。目前,我遇到的错误与标题相同。

出错的代码行是:

string[] strUserInitials = HttpContext.Current.Request.ServerVariables("LOGON_USER").Split(Convert.ToChar("\\"));

有人知道这是为什么吗?

我正在处理一个webservice(asmx文件)。

在代码顶部,我有以下内容:

using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

由于它是一个 WebService,你可以尝试使用 OperationContext 替代吗? - Widor
我现在收到了“当前上下文中不存在'OperationContext'名称”的错误。 - oshirowanen
我认为就是方括号[],正如@alun在下面发布的那样。 - slfan
3个回答

20

你需要引用System.Web并导入命名空间System.Web:

using System.Web;

我不会使用 Convert:

string[] strUserInitials = System.Web.HttpContext.Current.Request.ServerVariables["LOGON_USER"].Split('\\'));

13
你需要使用[]而不是()
string[] strUserInitials = System.Web.HttpContext.Current.Request.ServerVariables["LOGON_USER"].Split(System.Convert.ToChar(@"\"));

现在出现了“当前上下文中不存在'Convert'名称”的错误。 - oshirowanen
1
Convert位于系统命名空间中,因此要么将其更改为System.Convert,要么添加using System;。 - alun
1
@shirowanen:为什么要使用Convert?使用'\'即可。 - slfan
添加 System.Web.dll 引用,查看如何操作请点击此处,希望能对某些人有所帮助。 - Shaiju T

3

using System.Web;using System;放入源文件中...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接