简体   繁体   中英

Calling a method of the ASPX page from Web Service

I have a simple Web Service with one method. (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.

Can anyone ask, how can I do this?

you don't want to be sharing code in aspx pages. 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.

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.

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).

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.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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