httpcontext

a request level singleton object in asp.net

柔情痞子 提交于 2019-12-11 12:49:25
问题 I trying to write a kind of pseudo singleton implementation. I want it to work similar to how HttpContext does work, where I can get an instance to the context doing something as simple as: var ctx = HttpContext.Current; So my implementation goes something like this: public class AppUser { public string Username { get; set; } public string[] Roles { get; set; } public AppUser() { var appuser = HttpContext.Session["AppUser"] as AppUser; if(appuser == null) throw new Exception("User session has

HttpContext.Current is null in WCF Data Service hosted in Windows Service

家住魔仙堡 提交于 2019-12-11 04:16:42
问题 I created a WCF Data Service inside a Windows Service and tried to access the HttpContext. I added this to my config file: <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> </system.serviceModel> However, when I try to access it, it is null. protected override void OnStartProcessingRequest(ProcessRequestArgs args) { base.OnStartProcessingRequest(args); HttpContext httpContext = HttpContext.Current; File.AppendAllText(@"c:\Temp\ERROR.log", httpContext !=

How to perform a download instantly with ASP.NET C#?

删除回忆录丶 提交于 2019-12-11 02:04:56
问题 We have a rather large amount of data to be downloaded. Currently our system just outputs the stream (we do not use files) Code: HttpContext.Current.Response.AppendHeader("content-disposition", String.Format("attachment; filename={0}", filename)); HttpContext.Current.Response.BinaryWrite(ms.ToArray()); HttpContext.Current.Response.End(); However, it is using an extensive amount of memory on the server, waiting for the file to download.. I'd like to make it so it starts to download immediately

ASP.NET HttpContext Items vanish on server

不羁的心 提交于 2019-12-10 23:55:19
问题 We are currently seeing an issue with the use of HttpContext.Current.Items where the local environments of the developers show no issues (all works as expected) in the server environment we are suddenly loosing items placed inside (getting a NullReferenceException ). I sketched some code and use below: Global.asax we initialise the factory at BeginRequest: protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Items["Key"] = new Factory(); } Inside the

Is it possible to copy/clone HttpContext of a web request

落花浮王杯 提交于 2019-12-10 16:41:42
问题 What's the easiest way to clone current request's HttpContext instance? I'm developing an app in Asp.net MVC v1 . I upgraded the regular PartialView capabilities to actually have sub-controllers that act very similar, but have their own context. When you use PartialViews you have to fill view data for the partial view in your main view's controller action. I created my own functionality that makes it possible to call controller actions from within a view. This way I get: I don't have to

How to modify HttpContext.Request.Form in asp.net core

僤鯓⒐⒋嵵緔 提交于 2019-12-10 16:24:05
问题 I have an HttpContext.Request object that has data in the Form that is wrong and I want to fix it up and send the correct HttpContext on its way. HttpContext.Request.Form is readonly, but if it wasn't I would have simply done the following; HttpContext.Request.Form["a"] = "the correct value for a"; So, where is the best place in the pipeline to do this. Is it possible to make the HttpContext.Request.Form write accessable via reflection? 回答1: This was easier than I thought. I am doing this in

Alternatives to Response.TransmitFile()

牧云@^-^@ 提交于 2019-12-10 15:57:01
问题 So I have the set of code I've been toying with for the past couple days, I need to download a file from the server to the client. That's the easy part, but I also need to refresh a grid view after it's finished and display in an alert that the file has been successfully created, but every way that I've found to download contains a select line of code which will be my downfall. Response.End() Response.Close() or ApplicationInstance.CompleteRequest() All of these end the current response or I

System.Web.HttpContext.Current nulls itself after checking for a Cache

泪湿孤枕 提交于 2019-12-10 15:30:14
问题 I encountered a weird issue today which made no sense to me. Here is a summary: Inside a method, I check for a cached item as below: private async Task<RatesStatus> getRatesStatusAsync() { //... if (_currentHttpContext != null) { //Here, I am checking for a Cached item var cachedRatesStatusObj = HttpContext.Current.Cache[Constants.RATESSTATUS_CACHE_KEY_NAME]; if (cachedRatesStatusObj != null) return (RatesStatus)cachedRatesStatusObj; } //... cacheRatesStatusObject(ratesStatus); //... } Here,

How to mock HttpContext.User

邮差的信 提交于 2019-12-10 14:13:13
问题 I am working on a Asp.net MVC 5 project and I am trying to setup a mock to return a custom principal within a controller. I have search and tried different approach suggested but none of them works. I have a BaseController which all my controllers inherit from. The BaseController has a User property which return HttpContext.User in the getter. The HttpContext.user returns a value when called within the project but return a null when call from a unit test project. BaseController public class

HttpContext.Current.Items cleared via responseMode=“ExecuteURL”?

眉间皱痕 提交于 2019-12-10 04:52:49
问题 I avoid the default ASP.NET approach of redirecting on errors (as many people do). Clean AJAX code and SEO are among the reasons. However, I'm using the following method to do it, and it seems that I may lose HttpContext.Current.Items in the transfer? <httpErrors errorMode="Custom" existingResponse="Replace"> <remove statusCode="401" /> <remove statusCode="403" /> <remove statusCode="404" /> <remove statusCode="500" /> <error statusCode="401" responseMode="ExecuteURL" path="/Account/SignIn" /