MVC :: Passing Around HttpContext From HttpHandler.ProcessRequest?
Dec 24, 2010
I am working on an existing application which works using a custom built MVC framework.
The gateway into the front end is an implementation of IHttpHandler to handle the requests and determine which Action to call - its called FrontControllerHandler.
Currently FrontControllerHandler.ProcessRequest(HttpContext context) method takes a HttpContext object and then continues to pass it around to various other methods that it calls.
What we're hoping to refactor is to stop passing around the 'context' object and simply refer to:
HttpContext.Current Allowing asp.net to do the work for us...its there...why not use it, right?
Thing that i'm not sure about is, would there ever be a scenarion in which the HttpContext object passed to ProcessRequest would not actually refer to the same memory location (same object) that is referenced by:
HttpContext.CurrentI'm assuming that this would never happen unless something has gone wrong, in which case i can test for it and throw an exception. EG
[Code]....
So i would like to get peoples thoughts on this...
I have a strange issue where i have a a HttpHandler having its ProcessRequest() event firing twice.i have nothing else in the class except a pointer to a static method so i'm lost.I have done some googling to no avail even thought it appears a few people are having similar issues:
I am trying to pass an exception to an HttpHandler by doing the following:
catch (Exception e) { byte[] exceptionData; MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Persistence)); formatter.Serialize(stream, e); exceptionData = stream.ToArray(); WebClient client = new WebClient(); Uri handler = new Uri(ApplicationUri, "TransferException.axd"); #if DEBUG ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(BypassAllCertificateStuff); #endif try { client.UploadData(handler, exceptionData); } catch (WebException) { } }
EDIT
I am getting the following exception on the client.UploadData() line. "Content-Length or Chunked Encoding cannot be set for an operation that does not write data."
EDIT
Even if I change my call to be client.UploadString(location, "THIS IS A TEST!"); it still fails with the same exception.
We're migrating an application to use IIS7 integrated mode. In library code that is designed to work either within the context of an HTTP request or not, we commonly have code like this:
if (HttpContext.Current != null && HttpContext.Current.Request != null) { // do something with HttpContext.Current.Request } else { // do equivalent thing without HttpContext.. }
But in IIS7 integrated mode the check for HttpContext.Current.Request throws an exception whenever this code is called from Application_Start.
System.Web.HttpException: Request is not available in this context
How can I detect whether the request is really available without wrapping these calls in an exception handler and taking action based on whether an exception is generated or not.
Looking at HttpContext in Reflector I see it has an internal bool HideRequestResponse field but it's internal so I can only get to it with reflection and that's fragile. Is there a more official/approved way to determine if it's ok to call HttpContext.Request?
This blog post about the subject says not to use HttpContext, but how, in generic library code, can you determine if it's ok to use HttpContext?
I'm using the work-around mentioned there which is to use Application_BeginRequest and an initialized field to only initialize once as part of BeginRequest, but that has to be done in every calling application whereas I'd prefer to make the library code more robust and handle this situation regardless of where it's called from.
We have an ASP.NET web app talking to WCF for data. After resolving a number of service and db level performance issues, we turned to look at the web app itself.
After running a profile while hitting a few common pages, ProcessRequest is at the top of the list as far as elapsed exclusive time (which means time just in that method and any kernel code if I'm not mistaken)
Function Name Elapsed Inclusive Time Elapsed Exclusive Time System.Web.UI.Page.ProcessRequest(class System.Web.HttpContext) 31,573.64 17,915.56
Viewstate sizes are not severe, prob 60k at most for fattest page.
I have been experimenting with code that will clear all of the cookies in an HttpContext.Response.Initially, I used this:
DateTime cookieExpires = DateTime.Now.AddDays(-1); for (int i = 0; i < HttpContext.Request.Cookies.Count; i++) { HttpContext.Response.Cookies.Add( new HttpCookie(HttpContext.Request.Cookies[i].Name, null) { Expires = cookieExpires }); }
this will error with an OutOfMemoryException because the for loop never exits - each time you add a cookie to the Response, it also gets added to the `Request.
i'm using an example in which i can fake the session.It's for use in a unittest of a mvc controller.In the test i create a controller and then i do this:
FakeHttpContext httpctx = new FakeHttpContext(null,null,null,null,mSessionItems ); ControllerContext ctx2 = new ControllerContext(httpctx,new RouteData(), target);
here mSessionItems is my session and target is my instance of a controller,and indeed, when i'm in a controller reading this.ControllerContext.HttpContext.Session, i have a session, great!but.... i also read the session outside the controller, and there i use HttpContext.Current.Session, and that is null (or actualy, the HttpContext.Current is null).
In an ASP.NET application, I need to do some changes on every CSS file sent.So I created an HttpHandler (inside the app itself), added:
<add verb="*" path="*.css" type="MyWebsite.CssTestHandler,MyWebsite"/> to Web.config in system.web/httpHandlers and modified the handler like this: public void ProcessRequest(HttpContext context) { context.Response.Clear(); text.Response.Write("Hello World"); context.Response.End(); }
I have created a HttpHandler to be used with SWFUpload to upload images to the server. This upload is done in an administration backend so users need to be authenticated to upload images.Initially I made the whole administration area deny annonymous users but because of the way SWFUpload appears to work it wouldn't work correctly with Forms authentication and would return a 302 status code.I had thought it would be possible to make the location of my handler public in Web.config and use context.User.Identity.IsAuthenticated in my handler to determine if the user is logged in.
How can I tell from within an ASP.NET HttpHandler if it is executing because of a call to Server.Execute("myHandler.ashx")or because of the user linking directly to myHandler.ashx? (Besides using a querystring parameter).
As i know that asp.net fulfill all the requirements for any web application but what are the ground rules for creating custom httphandler and httpmodule in asp.net.Edit:For example I want to fetch image from database then what i should i use httphandler or normally read image from database.If httphandler then why?
Im trying to write a small web application for forwarding requests to my page to the new pages on my web site. First off im implementing a IHttpHandler and in the ProcessRequest method i simple want to print out the requesting page, my conde looks like this:
public class RedirectHandler : IHttpHandler { public bool IsReusable { get { return true; } [code]...
So I am working on a project in ASP.NET MVC 2 with C#.I have a View with a file upload but this is a flash component (.swf).The result of the file upload (the storing of the file and processing of data in the database) is done in a HTTPHandler.I would like the HTTPHandler to send information to my active controller.
Is there a way to get the controller that was used to load the view? I'm having problems finding information about it online.
I have an website in IIS 6.0 (windows 2003). Inside that I had configured another application (As IIS Application).
Now, When I uncommend a "add verb" tag in main web.config file, the webservice inside the application throws "404 file not found exception" Is there anything which I need to update in child web.config?
I'm trying to fashion a solution which will simulate App_Offline.htm for remote access but still allow local users to test the website out. I found some various options that I am trying out but the best one doesn't seem to work for our ASP.NET(2.0) site which relies on session state being enabled on all of the pages.
I'm using the RssToolkit for .net. I'm using an Httphandler to return rss feeds. The feedID is passed in as a querystring parameter. If no parameter is passed in, I would like to have the the handler return a feed that is an aggreate of some of the feeds that the handler can handle. What I'm wondering is, can the handler recurse?
Providing web features through a custom HttpHandler such as in Elmah is extremely handy for ASP.NET Web Applications, because the handler can be embedded into any ASP.NET web app. It perfectly fits as a simple way to extend an existing web app. Now, developing any significant set of features through a custom handler is a very tedious process. I am wondering if it is possible to directly embed an ASP.NET Application into another one through a custom handler (as opposed to cut and pasting the whole app in a sub directory). Here is a small list of embedded web app that would be fit for such a purpose:
Health monitoring console. Provisioning console (for cloud web app with auto-scaling). App settings management console (considering a scheme IoC-settings-stored-in-DB). Each one of those web parts could be provided as an HttpHandler; but again implementation is really tedious. Does anyone know how to do that or how to achieve an equivalent behavior?