{"id":40,"date":"2011-10-21T08:08:00","date_gmt":"2011-10-21T08:08:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2011\/10\/21\/placing-a-web-method-in-a-page\/"},"modified":"2011-10-21T08:08:00","modified_gmt":"2011-10-21T08:08:00","slug":"placing-a-web-method-in-a-page","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2011\/10\/21\/placing-a-web-method-in-a-page\/","title":{"rendered":"Placing a Web Method in a Page"},"content":{"rendered":"<div>In most cases, it makes sense to create a separate web service to handle your <a href=\"http:\/\/asp.net\/\">ASP.NET<\/a> AJAX callbacks.<\/div>\n<div>This approach generally results in clearer pages and makes it easier to debug and refine your code.<\/div>\n<div>However, in some situations you may decide you have one or more web methods that are designed<\/div>\n<div>explicitly for use on a single page and that really shouldn&#8217;t be reused in other parts of the application. In<\/div>\n<div>this case, you may choose to create a dedicated web service for each page, or you might choose to move<\/div>\n<div>the web service code into the page.<\/div>\n<div><\/div>\n<div>\n<div>Placing the web method code in the page is easy\u2014in fact, all you need is a simple bit of cut-andpaste.<\/div>\n<div>First, copy your web method (complete with the WebMethod attribute) into the code-behind class<\/div>\n<div>for your page. Then, change it to a static method, and add the System.Web.Script.Services.ScriptMethod<\/div>\n<div>attribute. Here&#8217;s an example where the web method (named GetTerritoriesInRegion) is placed in a web<\/div>\n<div>page named WebServiceCallback_PageMethods:<\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><span style=\"font-family: 'courier new', monospace;\">public partial class WebServiceCallback_PageMethods : System.Web.UI.Page<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">{<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">&nbsp; &nbsp;[System.Web.Services.WebMethod()]<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">&nbsp; &nbsp;[System.Web.Script.Services.ScriptMethod()]<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">&nbsp; &nbsp;public static List&lt;Territory&gt; GetTerritoriesInRegion(int regionID)<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">&nbsp; &nbsp;{<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">&nbsp; &nbsp; &nbsp; \/\/ Farm the work out to the web service class.<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">&nbsp; &nbsp; &nbsp;&nbsp;TerritoriesService service = new TerritoriesService();<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">&nbsp; &nbsp; &nbsp;&nbsp;return service.GetTerritoriesInRegion(regionID);<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">&nbsp; &nbsp;}<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">&#8230;<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">}<\/span><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div>Next, set the ScriptManager.EnablePageMethods property to true, and remove the reference in the<\/div>\n<div>&lt;Services&gt; section of the ScriptManager (assuming you don&#8217;t want to use any non-page web services):<\/div>\n<div><span style=\"font-family: 'courier new', monospace;\"><br \/><\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">&lt;asp:ScriptManager ID=&#8221;ScriptManager1&#8243; runat=&#8221;server&#8221; EnablePageMethods=&#8221;true&#8221;&gt;<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">&lt;\/asp:ScriptManager&gt;<\/span><\/div>\n<div><\/div>\n<div>Finally, change your JavaScript code so it calls the method through the PageMethods object, as<\/div>\n<div>shown here:<\/div>\n<div><span style=\"font-family: 'courier new', monospace;\"><br \/><\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\">PageMethods.GetTerritoriesInRegion(regionID, OnRequestComplete, OnError);<\/span><\/div>\n<div><span style=\"font-family: 'courier new', monospace;\"><br \/><\/span><\/div>\n<div>The PageMethods object exposes all the web methods you&#8217;ve added to the current web page.<\/div>\n<div>One advantage of placing a web method in a page is that the method is no longer exposed through<\/div>\n<div>an .asmx file. As a result, it&#8217;s not considered part of a public web service, and it&#8217;s not as easy for someone<\/div>\n<div>else to discover. This is appealing if you&#8217;re trying to hide your web services from curious users.<\/div>\n<div>Another reason you might choose to code your web methods in the page class is to read values from<\/div>\n<div>view state or the controls on the page. When you trigger a page method, a stripped-down version of the<\/div>\n<div>page life cycle executes, just like with the <a href=\"http:\/\/asp.net\/\">ASP.NET<\/a> client callback feature you saw in Chapter 29. Of<\/div>\n<div>course, there&#8217;s no point in trying to modify page details because the page isn&#8217;t being rerendered, so any<\/div>\n<div>changes you make will simply be discarded.<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In most cases, it makes sense to create a separate web service to handle your ASP.NET AJAX callbacks. This approach generally results in clearer pages and makes it easier to debug and refine your code. However, in some situations you may decide you have one or more web methods that are designed explicitly for use &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2011\/10\/21\/placing-a-web-method-in-a-page\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Placing a Web Method in a Page&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[32,34,35],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"In most cases, it makes sense to create a separate web service to handle your ASP.NET AJAX callbacks. This approach generally results in clearer pages and makes it easier to debug and refine your code. However, in some situations you may decide you have one or more web methods that are designed explicitly for use&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/40"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}