views:

276

answers:

1

I'm writing a component that I would like to be able to use in both MVC and WebForms web apps, but I'm not sure how to handle the differences between how HttpContext is handled.

My component involves a custom IHttpHandler (for WebForms) or a custom ActionResult (for MVC).

So I've got a few questions:

  • Is there a way to use an IHttpHandler with MVC without breaking the model?
  • Is it acceptable to use HttpContext.Current while attempting to write code that will work for both? It seems a little brute-force-ish to me (not sure why), but the alternative would be writing and implementing an fairly verbose interface to handle the abstraction between HttpContext and ControllerContext.
  • Am I going about this completely wrong?
+1  A: 
  1. Yes, you can just use routes.IgnoreRoute("MyHandler.ashx"). It'll fall back to the original ASP.NET handling without breaking the model.
  2. Nope, I think it's perfectly OK to use HttpContext.Current. I think when you are writing your own handler, MVC is not very applicable. You are writing your code for ASP.NET, not ASP.NET MVC or ASP.NET Web Forms. It's easy to make it working on both models (works OOTB with Web forms, with IgnoreRoute in MVC). You might making thing more complicated than they should be! Remember, the sole purpose of design patterns (such as MVC) is simplicity. Don't make things complicated!
  3. goto 2; Just write a simple handler!
Mehrdad Afshari
Good answer, thanks! :)
Daniel Schaffer