using web methods with master pages

…衆ロ難τιáo~ 提交于 2019-11-30 19:48:11

Your webmethod code cannot reside in the codebehind for your master page.

I've found it easier to include an actual web service or WCF service in my project for things that I will need to call from multiple pages.

EDIT :

To add a WCF Service to your project:

  1. right click the project
  2. select [WCF Service] and give it a name (ie. Agent.svc)
  3. set up the service (see http://www.codeproject.com/KB/aspnet/jQuery_To_WCF.aspx)

More than a few examples here on the Stackoverflow...

Hope that helps.

How about putting the webmethod in a base class?

public class WebMethodBase : Page
{
    <WebMethod> _
    Public Shared Function GetDate() As String
        Return DateTime.Now.ToString()
    End Function
}

Then inherit this class from those pages you want to expose the webmethod.

Forgive me for mixed C# and VB, i am not familiar with VB syntax.

You can go the Base Class method route, as Andreas mentioned above.

Make sure:

  • The webmethod is static.
  • In your Ajax call, dynamically change the URL based on the page you are viewing. Something like this:

(in Ajax call:)

url: window.location.pathname.substr(1) + "/GetDate"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!