我如何从一个ASPX.VB函数中调用一个ASHX? [英] How do I call an ASHX from inside an ASPX.VB function?

查看:481
本文介绍了我如何从一个ASPX.VB函数中调用一个ASHX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我做ASHX,通常是从JavaScript称为API获取值,但我需要调用它的权利在ASP.NET我想这不应该是一个问题,但我不知道的语法。​​

I need to get a value from an API I made with ASHX and normally it is called from javascript but I need to call it right in ASP.NET I figured this shouldn't be a problem but I'm not sure the syntax.

推荐答案

那么你有几个选项


  1. 您可以重构你的ASHX的code是在一个共享库,因此您可以直接访问方法等都可以处理程序。

  2. 您可以实例化处理程序并调用成员,如果他们不是私有的。

  3. 您可以创建一个WebRequest的的处理程序和处理响应。

这些只是几个简单的方法。

These are just a few of the easy ways.

我个人很喜欢,因为它促进code重用,但根据情况,你可以做你喜欢的第一个方法。

I personally like the first method because it promotes code reuse, but depending on scenario you can do what you like.

编辑在评论的问题提供答案。

Edit to provide answers for question in comment.

从本质上讲是...而不是在您的处理程序有一堆code的你犯了一个叫有意义的东西给你上下文类。该类里面放置那是在您的处理程序中的逻辑。然后从处理程序,您可以创建一个实例或(取决于你如何实现它),它传递的HttpContext对象或任何为逻辑正常运行,需要调用类的静态版本。做同样的事情在你的ASPX页面。现在,您可以调用包含从任何逻辑在你的应用程序而不是它驻留在单独处理的对象。

Essentially Yes... Instead of having a bunch of code in your handler you make a class called something meaningful to you contextually. Inside that class you place the logic that was in your handler. Then from your handler you can create an instance or call a static version of the class (depending on how you implemented it) passing it the HttpContext object or whatever is required for that logic to run correctly. Do the same thing in your ASPX page. You can now call into an object that contains the logic from anywhere in your app instead of having it reside in the handler alone.

EX:

Public Class MyCommonLogic
    Public Shared Function ReturnSomethingCommon(context As HttpContext) As String
        Return "Hello World!"
    End Function
End Class

然后从处理程序或aspx页面..

Then from the handler or the aspx page..

Dim something As String = MyCommonLogic.ReturnSomethingCommon(...)

我做了静态的功能,但是这仅仅是当然的一个例子,我会实现它可是会更有意义在您的方案。

I made the function static, but that is just an example of course I would implement it however would make more sense in your scenario.

改code到VB抱歉。

Changed code to VB sorry about that.

这篇关于我如何从一个ASPX.VB函数中调用一个ASHX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆