Tag: httpcontext

在unit testing中使用httpcontext

我正在使用C#4.0,我需要对服务进行unit testing。 服务中的函数返回一个类似于我预期的变量i的路径,这是我期望回来的路径。 但是,当我运行此测试时,我得到HttpContext.Current为NULL的错误。 我该怎么做才能解决这个问题,以便可以运行测试? [TestMethod] public void GetPathTest() { var expected = System.IO.Path.GetFullPath(HttpContext.Current.Server.MapPath(“~/Certificates/”)); var path = _mockService.Setup(o => o.GetPath()).Returns(expected); }

将Http Request读入Byte数组

我正在开发一个需要获取HTTP Post请求并将其读入字节数组以进行进一步处理的网页。 我有点坚持如何做到这一点,我很难理解什么是最好的方法。 这是我到目前为止的代码: public override void ProcessRequest(HttpContext curContext) { if (curContext != null) { int totalBytes = curContext.Request.TotalBytes; string encoding = curContext.Request.ContentEncoding.ToString(); int reqLength = curContext.Request.ContentLength; long inputLength = curContext.Request.InputStream.Length; Stream str = curContext.Request.InputStream; } } 我正在检查请求的长度及其总字节数等于128.现在我只需要使用Stream对象将其转换为byte []格式吗? 我正朝着正确的方向前进吗? 不知道如何继续。 任何建议都会很棒。 我需要将整个HTTP请求放入byte []字段。 谢谢!

HttpContext.Current.User.Identity.Name如何知道存在哪些用户名?

这不一定是一个问题,我只是好奇它是如何工作的。 我有一个方法: public static bool UserIsAuthenticated() { bool isAuthed = false; try { if (HttpContext.Current.User.Identity.Name != null) { if (HttpContext.Current.User.Identity.Name.Length != 0) { FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity; FormsAuthenticationTicket ticket = id.Ticket; isAuthed = true; string MyUserData = ticket.UserData; } } } catch { } // not authed return isAuthed; } 如果用户不存在, HttpContext.Current.User.Identity.Name将返回null ,但它如何知道哪些用户名存在或不存在?

在一次调用中Request.Url.Host和ApplicationPath

有没有办法在一次调用中获取HttpContext.Current.Request.Url.Host和HttpContext.Current.Request.ApplicationPath ? 像“完整的应用程序URL”之类的东西? 编辑:澄清 – 我需要的是[]中的部分: http://[www.mysite.com/mywebapp]/Pages/Default.aspx 我只是出于好奇而问。 编辑2:感谢所有的回复,但没有一个是我正在寻找的。 仅供参考,我以这种方式解决了问题(但我仍然有兴趣知道是否有更顺畅的方式): public string GetWebAppRoot() { if(HttpContext.Current.Request.ApplicationPath == “/”) return “http://” + HttpContext.Current.Request.Url.Host; else return “http://” + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath; }

在Console应用程序中人工填充HttpContext对象

我正在为log4net编写一个包装器库。 该库应该能够捕获上下文信息,如查询字符串,cookie,表单字段等。 我从Console应用程序调用此包装类而不是TDD类。 有没有办法在Console应用程序中填充HttpContext对象,如下所示? HttpContext c = new HttpContext(null); c.Request.QueryString.Keys[1] = “city”; c.Request.QueryString[1] = “Los Angeles”; c.Request.QueryString.Keys[2] = “state”; c.Request.QueryString[2] = “CA”; 然后按如下方式检索它? Console.WriteLine(context.Request.QueryString.Keys[1]); 当我尝试上面的代码时,我收到以下exception。 Property or indexer ‘System.Collections.Specialized.NameObjectCollectionBase.KeysCollection.this[int]’ cannot be assigned to — it is read only

HttpContext在类库中不可用

我正在开发一个项目,我有一个需要使用System.web.HttpContext的C#类库。 我之前在另一个项目中做过这个没有问题,但现在它没有工作。 我不确定我错过了什么,他们都瞄准.net 3.5,我添加了参考System.web并using System.web添加了指令。 但是,当我尝试做HttpContext没有找到任何东西。 我已经尝试使用完整路径System.web.HttpContext但唯一出现的是与ASP相关的3个项目。 以下是工作项目智能感知和非工作智能感知的截图 下面是工作截图 以下是非工作截图 感谢您的任何帮助,您可以提供

静态方法里面的HttpContext.Current.Response

我在静态类中有以下静态方法。 我的问题是在静态方法中使用HttpContext.Current.Response是否安全? 我想100%确定它是线程安全的,并且只与调用线程相关联。有人知道答案吗? public static void SetCookie(string cookieName, string cookieVal, System.TimeSpan ts) { try { HttpCookie cookie = new HttpCookie(CookiePrefix + cookieName) {Value = cookieVal, Expires = DateTime.Now.Add(ts)}; HttpContext.Current.Response.Cookies.Add(cookie); } catch (Exception) { return; } }

MVC5 Ninject绑定和HttpContext

我正在尝试建立一个新项目,并且我添加了一个新类MembershipService ,它需要在它的构造函数中传递HttpContext 。 在之前的项目中,我使用了代码 private static void RegisterServices(IKernel kernel) { kernel.Bind() .To() .InRequestScope() .WithConstructorArgument(“context”, HttpContext.Current); …. } 然而,在新项目中,我正在使用Ninject Modules,在对StackOverflow和Google进行一些搜索之后,我提出了以下代码:public class ServiceHandlerModule:NinjectModule { public override void Load() { Bind() .To() .WithConstructorArgument(“context”, ninjectContext=> HttpContext.Current); this.Kernel.Bind(x => { x.FromAssemblyContaining(typeof(NinjectWebCommon)) .SelectAllClasses() .Where(t => t != typeof(MembershipService)) .BindDefaultInterface(); }); this.Kernel.Bind(x => { x.FromAssemblyContaining() .SelectAllClasses() .Where(t => t != typeof(MembershipService)) .BindDefaultInterface(); […]

对HttpContext.Current.Session的静态引用是否为所有用户返回相同的会话?

在同一Web应用程序的多个用户方面,以下代码是否存在问题? 我的意思是,我知道一个纯静态字符串将在单个ASP.NET应用程序的所有会话中共享,但鉴于这明确引用了Current.Session ,即使它是静态的,它似乎总是引用“当前用户”的会话实例。 但是一个错误正在发生,可以解释为每个人共享Mode的当前值,从而最近的更改覆盖其他人的模式值。 (作为背景:此字符串位于整个应用程序中使用的Helpers类中。我不希望在整个应用程序中对Session[“Mode”]进行硬编码引用,并且不希望必须传递Session[“Mode”]来自aspx.cs页面的每个方法调用中的Session[“Mode”] 。) public static string Mode { get { var value = HttpContext.Current.Session[“Mode”]; return (value ?? string.Empty).ToString(); } set { HttpContext.Current.Session[“Mode”] = value; } }

异步任务中的HttpContext.Current null

我有一个使用存储库( userRepo )的方法: public override Task CreateLocalUserAsync(IUser user, string password, CancellationToken cancellationToken) { var task = new Task(() => { TUserEntity newUser = new TUserEntity { Id = user.Id, UserName = user.UserName, Password = password }; userRepo.Save(newUser).Flush(); return new IdentityResult(true); }, cancellationToken); task.Start(); return task; } userRepo对象具有使用HttpContext.Current的依赖项。 使用ninject InRequestScope解决了这两个问题。 在Mvc 5中的默认AccountController中调用上面的方法: var result = await […]