locked
Asp.net page methods not working properly in VS 2013 RRS feed

  • Question

  • User1871575446 posted

    Hello,

    I've just installed VS 2013 and I have some problems with page methods.

    I've created a new "ASP.NET Web Forms Website" and added a new asp page using master page.

    In code behind file I have the following web method:

        [WebMethod]
        public static string MyMethod(string world)
        {
            return "Hello" + world;
        }

    In the aspx page I have the following code:

    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" Runat="Server">
    
    
        <asp:Button ID="Button1" runat="server" Text="Button" CssClass="test" />
            <script type="text/javascript">
    
                $(".test").click(function (e) {
                    CallMyMethod();
                    return false;
                });
    
    
    
                function MyMethod_Result(ResultString) {
                    alert(ResultString);
                }
    
                function CallMyMethod() {
                    PageMethods.MyMethod("World", MyMethod_Result);
                }
        </script>
    
    </asp:Content>

    And in the master page i've enabled page methods.

     <asp:ScriptManager runat="server" EnablePageMethods="true">
    

    The problem is that MyMethod function returns the whole page code; I've searched the entire web, found many sollutions but still I can't make this work, and this is just a simple code in a default website.

    I've also do the same thing in VS 2012 and there it works. 

    I intend to believe that VS 2013 is an upgraded version of VS 2012, but I've experienced the opposite. 

    On some search results I've seen that I must add this to web.config file, but I get the same result.

        <httpModules>
          <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </httpModules>

     

    What I do wrong?

    Saturday, February 8, 2014 8:06 AM

All replies

  • User-417640953 posted

    Hi gawrrell,

    Thank you post the issue to asp.net forum.

    With your description and code provided, I see you want to call the page methods from javascript using the PageMethods.

    And it returns whole page. I test the code you given above and it works fine in my side.

    As you mentioned, add below configuration to your web.config file.

    <system.web>
      <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </httpModules>
    </system.web>

    Rebuilt it and try again. Besides, you should try to check the version of .net framework.

    Hope this helps, thanks.

    Best Regards!

    Sunday, February 9, 2014 10:23 PM
  • User1871575446 posted

    This is what VS 2013 is generating when I create a new Website using Forms and I've added the httpModules

    <?xml version="1.0" encoding="utf-8"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
      
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-WebSite2-24f8204b-e46c-4099-9a72-cef173d9dc10;AttachDbFilename=|DataDirectory|\aspnet-WebSite2-24f8204b-e46c-4099-9a72-cef173d9dc10.mdf;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
      </connectionStrings>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
    
        <!-- ADDED BY ME TO FIX PAGE METHODS -->
        <httpModules>
          <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </httpModules>
    
    
        <pages>
          <namespaces>
            <add namespace="System.Web.Optimization" />
            <add namespace="Microsoft.AspNet.Identity" />
          </namespaces>
          <controls>
            <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
          </controls>
        </pages>
        <membership>
          <providers>
            <!--
    	      ASP.NET Membership is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
            -->
            <clear />
          </providers>
        </membership>
        <profile>
          <providers>
            <!--
    	      ASP.NET Membership Profile is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
            -->
            <clear />
          </providers>
        </profile>
        <roleManager>
          <!--
    	        ASP.NET Membership Role is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
            -->
          <providers>
            <clear />
          </providers>
        </roleManager>
        <!--
                If you are deploying to a cloud environment that has multiple web server instances,
                you should change session state mode from "InProc" to "Custom". In addition,
                change the connection string named "DefaultConnection" to connect to an instance
                of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
          -->
        <sessionState mode="InProc" customProvider="DefaultSessionProvider">
          <providers>
            <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
          </providers>
        </sessionState>
    
      </system.web>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="v11.0" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>  
    </configuration>

    Then I go to Build -> Rebuild Sollution, and when I run(F5) the project I get another error:

    HTTP Error 500.22 - Internal Server Error

    An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode.

    Searching the web for this error I've found that the most common fix is by adding this code:

      <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
      </system.webServer>  

    And finally, with this piece of code added, I go back to my previous error, the page method return the whole page script.

    PS.

    I've got Windows 8.1, Visual Studio 2013 and IIS 8 installed.

    Monday, February 10, 2014 12:19 PM
  • User-417640953 posted

    <pages>
          <namespaces>
            <add namespace="System.Web.Optimization" />
            <add namespace="Microsoft.AspNet.Identity" />
          </namespaces>
          <controls>
            <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
          </controls>
        </pages>
    

    Hi gawrrell,

    Thanks for your response.

    Please try to remove the "System.Web.Optimization" and "Microsoft.AspNet.Web.Optimization.WebForms" from your web.config.

    http://stackoverflow.com/questions/21091935/webmethod-not-called-triggered-by-pagemethod-in-visual-studio-2013

    Hope this helps, thanks.

    Regards!

    Monday, February 10, 2014 9:16 PM
  • User1871575446 posted

    Thanks for your quick replays. I've removed those lines of code from web.config then I've rebuild the entire sollution and still the same error.

    If you want I can send you the project.

    Tuesday, February 11, 2014 5:49 PM
  • User-417640953 posted

    Hi,

    If necessary, you can upload the project online.

    That will be easily for other members undersand your issue by download the project.

    Thanks.

    Tuesday, February 11, 2014 9:06 PM
  • User-1603191330 posted

    gawrrell

    I am having issues with Page Methods not working in VS2013.  I am getting PageMethods is not defined.  I have all the necessary settings set like EnablePageMethods on the ScriptManager, WebMethod attribute, public static method and also the module in the web.config.   Was you able to fix your issue.  Any help would be greatly appreciated.

    Wednesday, May 27, 2015 1:41 PM