WebMethod not called when url rewrite active
Asked Answered
P

3

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?

Psychologize answered 8/5, 2012 at 13:45 Comment(2)
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.Psychologize
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.Mirage
S
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?

Smriti answered 8/5, 2012 at 15:31 Comment(4)
Thanks, so you are basically saying that WebMethods dont work together with url rewriting?Psychologize
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.Smriti
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".Psychologize
Solved, solution is to use an asmx file instead.Psychologize
C
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.

Cybele answered 8/5, 2012 at 16:30 Comment(2)
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 :)Psychologize
I suppose it was merely doing what you were already trying with jQuery, so it would not work. My suggestion was foolish, I apologize.Cybele
I
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.

Iodide answered 9/7, 2019 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.