6

I know there are simular posts out there, but I found no help in any of them.

My web methods work when im not using url rewriting, but as soon as I turn it on it stop working.

jQuery

        $.ajax({
            type: "POST",
            url: "index.aspx/SaveSetting",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                console.log(msg);
            }
        });

C#

    [WebMethod()]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public static string SaveSetting()
    {
        return "OK";
    }

When this is called, I get the full HTML of my page back, and no "OK" message. I ran the debugger and saw that when I call the web method it triggers Page_Load in my page and not the web method.

So I got the corerct path, but the web method is not called.

I use C#, jQuery, ASP.NET 3.5.

Any help?

Martin at Mennt
  • 5,677
  • 13
  • 61
  • 89
  • Okay, found out that a rewrite rule is messing this up. So guess the best way is to use a *.asmx file as ScottE suggests, at least that work. – Martin at Mennt May 08 '12 at 19:40
  • i knew this is very old post. I just want to know did you found any solution for url rewrite and webmethod? .asmx is working perfectly with url rewrite, but that is not helping in my project. in my project it is required to run Webmethod with url rewrite. – JSB Nov 23 '14 at 20:18

3 Answers3

2

You'll need to use an complete link to your web method.

If you look in firebug you'll see, for example:

http://localhost/test1/index.aspx/SaveSetting as the url that you're trying to request, assuming that /test1 rewrites to /index.aspx

Assuming that the page lives at the root of your site, the following will work:

url: /index.aspx/SaveSetting

(This doesn't work at all with url routing, by the way!)

Perhaps move your web methods into an asmx file instead?

ScottE
  • 21,530
  • 18
  • 94
  • 131
  • Thanks, so you are basically saying that WebMethods dont work together with url rewriting? – Martin at Mennt May 08 '12 at 16:26
  • They work just fine as long as the pathing is correct. If you use a asmx web service instead of code-behind web methods, or a pull path to your aspx page you should be fine. – ScottE May 08 '12 at 16:32
  • I have used firebug, and I got the full path ´/website/admin/settings/index.aspx/SaveSetting´ But when I try to open this link in my browser I get the usual index.aspx not the web method. It is not like I get a 404, I do get the full HTML from the page back from the ajax call, but I should only get "OK". – Martin at Mennt May 08 '12 at 19:21
  • Solved, solution is to use an asmx file instead. – Martin at Mennt May 08 '12 at 19:41
0

If you add this to your javascript:

PageMethods.set_path("/whatever/the/actual/path/is/index.aspx");

Then your standard webmethod calls should work:

PageMethods.SaveSetting(console.log);

My answer was taken from this question that was previously asked.

Community
  • 1
  • 1
Nick
  • 226
  • 1
  • 7
  • I tried this, and it does not help. My path is correct, I do get the full HTML of the page back from the ajax call. But the problem is that the web method is not invoked, only the page_load. And by the way, it is PageMethods.set_path not PageMethods.set_page :) – Martin at Mennt May 08 '12 at 19:26
  • I suppose it was merely doing what you were already trying with jQuery, so it would not work. My suggestion was foolish, I apologize. – Nick May 08 '12 at 20:01
0

Just a follow-up in case anyone else stumbles on this. There's also something required in the web.config to ensure IIS understands how to handle the request of the webmethod, rather that just the page itself:

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

Without this line I had the exact same issue - the server would return the entire FRONT web page, rather than the response from the webmethod.

There could also be other things in the web.config that are required. Mine is enourmous (ha!!) so who knows what else might be influencing this issue.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
David
  • 1