HttpContext 并为 WebForms 和 MVC 编写组件

发布于 2024-07-10 19:33:16 字数 405 浏览 9 评论 0原文

我正在编写一个组件,我希望能够在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2024-07-17 19:33:16
  1. 是的,您可以只使用routes.IgnoreRoute("MyHandler.ashx")。 它将回退到原始 ASP.NET 处理,而不会破坏模型。
  2. 不,我认为使用 HttpContext.Current 完全没问题。 我认为当你编写自己的处理程序时,MVC 不太适用。 您正在为 ASP.NET 编写代码,而不是为 ASP.NET MVC 或 ASP.NET Web 窗体编写代码。 让它在两种模型上工作都很容易(OOTB 与 Web 表单一起工作,与 MVC 中的 IgnoreRoute 一起工作)。 你可能会让事情变得比应有的更复杂! 请记住,设计模式(例如 MVC)的唯一目的是简单性。 别把事情搞复杂了!
  3. goto 2; 只需编写一个简单的处理程序即可!
  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!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文