HttpContext 并为 WebForms 和 MVC 编写组件
我正在编写一个组件,我希望能够在 MVC 和 WebForms Web 应用程序中使用该组件,但我不确定如何处理 HttpContext 处理方式之间的差异。
我的组件涉及自定义 IHttpHandler(对于 WebForms)或自定义 ActionResult(对于 MVC)。
所以我有几个问题:
- 有没有办法在不破坏模型的情况下将 IHttpHandler 与 MVC 结合使用?
- 在尝试编写适用于两者的代码时使用 HttpContext.Current 是否可以接受? 对我来说这似乎有点暴力(不知道为什么),但另一种选择是编写和实现一个相当详细的接口来处理 HttpContext 和 ControllerContext 之间的抽象。
- 我的做法完全错误吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
routes.IgnoreRoute("MyHandler.ashx")
。 它将回退到原始 ASP.NET 处理,而不会破坏模型。goto 2;
只需编写一个简单的处理程序即可!routes.IgnoreRoute("MyHandler.ashx")
. It'll fall back to the original ASP.NET handling without breaking the model.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!goto 2;
Just write a simple handler!