4

I have a simple webpage which has a WebMethod in it. But it's not working even after I tried everything I found on Google. When I go to http://server/test.aspx/Test through browser, It returns entire page even if the webMethod is removed. This is the code:

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.UI;
   using System.Web.UI.WebControls;

   namespace IgnisAccess
   {
       public partial class test : System.Web.UI.Page
       {

           protected void Page_Load(object sender, EventArgs e)
           {

           }

           [System.Web.Services.WebMethod]
           public static string Test()
           {
               return "Success";
           }
       }
   }

This is the Design

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="IgnisAccess.test" %>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
     <head runat="server">
       <title></title>
     </head>
     <body>
        <form id="form1" runat="server">
           <div>
           </div>
        </form>
     </body>
   </html>

I have tried adding this Web.Config entry too, but of no use.

 <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>
6
  • 2
    What are you expecting to see here? I believe the default method to access a webmethod is via a POST, not a GET. Commented Apr 21, 2015 at 13:05
  • 2
    Hi, you need to use webserive(ASMX) or WCF for this Commented Apr 21, 2015 at 13:06
  • Can you elaborate on it's not working? That doesn't really tell us anything. Commented Apr 21, 2015 at 13:07
  • 2
    @Paddy I am expecting the returned string back. I have tried POST using "Advanced REST Client App for Chrome". It also recieves the whole page, and not hitting the method. Commented Apr 21, 2015 at 13:08
  • 3
    @LokeshBR I don't think I need to create a WCF service for getting some results in my JQuery Ajax. Commented Apr 21, 2015 at 13:18

1 Answer 1

5

try this one

[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string Test()
{
    return "Success";
}

and make sure its POST not GET

Sign up to request clarification or add additional context in comments.

1 Comment

This works.. :) problem was with the response format. Thanks so much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.