当前上下文中不存在名称"HttpContext"

osh*_*nen 7 .net c# asp.net asmx .net-3.5

我试图将一些vb.net转换为C#,但我一直在收到错误.目前,我在标题中收到错误.

问题在于:

string[] strUserInitials = HttpContext.Current.Request.ServerVariables("LOGON_USER").Split(Convert.ToChar("\\"));
Run Code Online (Sandbox Code Playgroud)

任何人都知道为什么会这样吗?

我正在开发一个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;
Run Code Online (Sandbox Code Playgroud)

slf*_*fan 17

您必须引用System.Web并导入命名空间System.Web:

using System.Web;
Run Code Online (Sandbox Code Playgroud)

我根本不会使用Convert:

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


alu*_*lun 11

你需要[]而不是():

string[] strUserInitials = System.Web.HttpContext.Current.Request.ServerVariables["LOGON_USER"].Split(System.Convert.ToChar(@"\"));
Run Code Online (Sandbox Code Playgroud)