Tag: httpcontext

Response.TransmitFile()的替代方案

所以我有一套我过去几天一直在玩的代码,我需要从服务器下载一个文件到客户端。 这是一个简单的部分,但我还需要在完成后刷新网格视图并在警报中显示文件已成功创建,但我发现下载的每一种方式都包含一行代码,这将是我的倒台。 到Response.End() Response.Close()或 ApplicationInstance.CompleteRequest() 所有这些都结束了当前的响应,或者我相信在ApplicationInstance的情况下,它将页面的所有源代码刷新到我试图下载的文本文件中。 下面是我从服务器下载文件的代码片段,以下是下载文件的源代码。 如果你有任何可以帮助解决这个永无止境的噩梦,那将非常感激。 //I brought everything together in an arraylist to write to file. asfinalLines = alLines.ToArray(typeof(string)) as string[]; string FilePath = HttpContext.Current.Server.MapPath(“~/Temp/”); string FileName = “test.txt”; // Creates the file on server File.WriteAllLines(FilePath + FileName, asfinalLines); // Prompts user to save file System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.ClearContent(); response.Clear(); response.ContentType […]

如何模拟HttpContext.User

我正在开发一个Asp.net MVC 5项目,我正在尝试设置一个模拟以在控制器中返回自定义主体。 我有搜索并尝试了不同的方法,但没有一个有效。 我有一个BaseController,我的所有控制器都inheritance自。 BaseController有一个User属性,它在getter中返回HttpContext.User。 HttpContext.user在项目中调用时返回一个值,但在从unit testing项目调用时返回null。 BaseController public class BaseController : Controller { protected virtual new CustomPrincipal User { get { return HttpContext.User as CustomPrincipal; } ***<== Line with issue*** } } 自定义校长 public class CustomPrincipal : IPrincipal, ICustomPrincipal { public IIdentity Identity { get; private set; } public string UserId { get; […]

“System.Web.HttpException:此上下文中的响应不可用”仅在IIS中

我在global.asax.cs Application Start中使用了Http.Current.Response 。 当我在我的电脑上执行时,它工作正常没有任何问题。 但是,当我尝试将其与Windows Server 2008 R2一起放入IIS时,我发现它发出以下错误。 它的代码是 public static void SetCookie(string key, string value, int dayExpires) { HttpCookie encodedCookie = HttpSecureCookie.Encode(new HttpCookie(key, value)); encodedCookie.Expires = DateTime.Now.AddDays(dayExpires); HttpContext.Current.Response.Cookies.Remove(key); HttpContext.Current.Response.Cookies.Add(encodedCookie); } 我想找出为什么它在我的系统中执行,而不是在IIS中执行。 谢谢

在Parallel.For内部调用的方法中,HttpContext为null

尝试了很多后发布这个问题。 做正常不是一种选择,因为我们需要在很短的时间内完成大量的处理。 我有GetDataFor() ,其中使用了HttpContext.Current 。 代码如下所示: public void SomeMethod() { var context = HttpContext.Current; Parallel.For(0, 100, i => { var data = GetDataFor(i, context); }); } public data GetDataFor(int i, HttpContext context) { Uri requestUri = null; if (HttpContext.Current != null) { requestUri = HttpContext.Current.Request.Url; sCookie = string.Format(“{0}”, HttpContext.Current.Request.Headers[“cookie”]); } else { requestUri = context.Request.Url; […]

将List 保存到ASP.NET中的会话

CartItems保存在SQL数据库中。 我想将所有CartItem放入List并转移到Instance.Items。 实例变量将保存到会话中。 代码如下。 public class ShoppingCart { public List Items { get; private set; } public static SqlConnection conn = new SqlConnection(connStr.connString); public static readonly ShoppingCart Instance; static ShoppingCart() { if (HttpContext.Current.Session[“ASPNETShoppingCart”] == null) { Instance = new ShoppingCart(); Instance.Items = new List(); HttpContext.Current.Session[“ASPNETShoppingCart”] = Instance; } else { Instance = (ShoppingCart)HttpContext.Current.Session[“ASPNETShoppingCart”]; } } […]

如何初始化自定义HTTP上下文或HttpContextBase

我正在尝试创建自己的自定义HTTP上下文: CustomHttpContext : HttpContextBase { public override HttpRequestBase Request { } } 我无法弄清楚的一件事是如何初始化基类 System.Web.HttpContext.Current 有没有人有任何想法我如何首先使用Current Http初始化自定义上下文然后覆盖某些方法/属性以满足我自己的目的?

如何将HttpContext.Current传递给在.net中使用Parallel.Invoke()调用的方法

我有两个方法,它们使用HttpContext.Current来获取userID。 当我单独调用这些方法时,我得到userID,但是当使用Parallel.Invoke()调用相同的方法时,HttpContext.Current为null。 我知道原因,我只是在寻找可以访问HttpContext.Current的工作。 我知道这不是线程安全的,但我只想执行读操作 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Display(); Display2(); Parallel.Invoke(Display, Display2); } public void Display() { if (HttpContext.Current != null) { Response.Write(“Method 1” + HttpContext.Current.User.Identity.Name); } else { Response.Write(“Method 1 Unknown” ); } } public void Display2() { if (HttpContext.Current != null) { Response.Write(“Method […]

在HttpContext.Current.Request中模拟ServerVariables

我的一个服务使用IIS通过此类代码提供的服务器变量 var value = System.Web.HttpContext.Current.Request.ServerVariables[“MY_CUSTOM_VAR”]; 我试过的是模拟那些对象并插入我自己的变量/集合并检查几个案例(例如变量丢失,值为null …)我能够创建HttpContext,HttpRequest,HttpResponse的实例并分配它们但是它们中的每一个都只是一个没有接口或虚拟属性的普通类,ServerVariables的初始化发生在某个地方。 HttpContext嘲笑: var httpRequest = new HttpRequest(“”, “http://excaple.com/”, “”); var stringWriter = new StringWriter(); var httpResponse = new HttpResponse(stringWriter); var httpContextMock = new HttpContext(httpRequest, httpResponse); HttpContext.Current = httpContextMock; 尝试#1通过reflection调用私有方法 var serverVariables = HttpContext.Current.Request.ServerVariables; var serverVariablesType = serverVariables.GetType(); MethodInfo addStaticMethod = serverVariablesType.GetMethod(“AddStatic”, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic); addStaticMethod.Invoke(serverVariables, new object[] […]

HttpContext类及其线程安全性

我在应用程序中有一个具有以下属性的Singleton对象: private AllocationActionsCollection AllocationActions { get { return HttpContext.Current.Session[“AllocationOptions.AllocationActions”] as AllocationActionsCollection; } set { HttpContext.Current.Session[“AllocationOptions.AllocationActions”] = value; } } 我正在处理一个错误( HttpContext.Current.Session [“AllocationOptions.AllocationActions”]为null,即使它应该总是设置为有效的实例…)。 我刚刚在MSDN中读到HttpContext实例成员不保证是线程安全的! 我想知道这是不是问题。 应用程序中某处可能存在资源竞争,而HttpContext.Current.Session [“AllocationOptions.AllocationActions”]为空的时刻是使用此语句使用AllocationActions setter的时刻: AllocationActions = new AllocationActionsCollection(Instance.CacheId); 我的问题是: a)我很震惊HttpContext.Current.Session不是线程安全的。 那么如何安全地使用那个属性呢? b)你有什么想法为什么Session变量可以为null(即使我很确定我在第一次使用它之前设置它)? 谢谢你,帕维尔 编辑1: a)使用以下语句每隔2分钟设置初始化会话变量的行(在Page_Load中执行) AllocationActions = new AllocationActionsCollection(Instance.CacheId); b)调用getter的代码在事件处理程序中调用(如Button_Click) c)应用程序中没有自定义线程。 只有常见的HTTP Handler

在不使用HttpContext.Current的情况下确定URL主机名?

使用当前请求,我可以获取URL主机名: HttpContext.Current.Request.Url.Host 但是 – 我需要在不使用当前请求( HttpContext.Current )的情况下确定URL主机名。 这样做的原因是我的代码是从onChange回调中的SqlDependency调用的,当时发现了SQL依赖关系。 虽然代码驻留在我的Web应用程序中,但没有请求,并且HttpContext.Current为null。 我希望我可以从HttpRuntime获取它,但似乎没有任何用处。 有没有办法可以获得这些信息?