简体   繁体   English

从Web Service调用ASPX页面的方法

[英]Calling a method of the ASPX page from Web Service

I have a simple Web Service with one method. 我有一种方法的简单Web服务。 (This method calling from another application) In this method I need to call ASPX page of sided site - not html code, but method of this page. (此方法从另一个应用程序调用)在此方法中,我需要调用辅助站点的ASPX页面-不是html代码,而是此页面的方法。

Can anyone ask, how can I do this? 谁能问,我该怎么做?

you don't want to be sharing code in aspx pages. 您不想在aspx页面中共享代码。 You should create a project of type class library in your solution. 您应该在解决方案中创建一个类库类型的项目。 Put your code that you want to be common code called from aspx pages and web services into the class project in a class. 将您希望成为通用代码的代码(从aspx页面和Web服务调用)放入类的类项目中。

It is generally a good idea to have shared/reusable code stored separately from both the web service and the ASPX page so that any number of dependents may access it. 通常最好将共享/可重用的代码与Web服务和ASPX页面分开存储,以便任何数量的从属都可以访问它。

But to answer your question specifically: 但要专门回答您的问题:

I'll assume that the web service class is able to reference the ASPX page class (.aspx.cs or aspx.vb). 我假设Web服务类能够引用ASPX页面类(.aspx.cs或aspx.vb)。

The example below shows two ways of accomplishing what you're asking. 下面的示例显示完成要求的两种方法。 You can either instantiate your Page class and use the method just as you would any other normal class, or if the method is static, use it as is without instantiating your Page class. 您可以实例化Page类并像使用其他任何普通类一样使用该方法,或者,如果该方法是静态的,则按原样使用它而无需实例化Page类。

Example: 例:

public class MyAspxPage : Page
{
    private Object _myObj = new object();

    public object GetObject()
    {
        return _myObj;
    }

    public static object GetAnObject()
    {
        return new object();
    }
}

public class MyWebService : WebService
{
    public void MyWebServiceMethod1()
    {
        MyAspxPage page = new MyAspxPage();
        object result = page.GetObject();
    }

    public void MyWebServiceMethod2()
    {
        object result = MyAspxPage.GetAnObject();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM