Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PageMethods in ASP.NET failed to work if you have ASP.NET Routing Implemented

I have a web method:

[System.Web.Services.WebMethod]
public static string getCharacterstics(int product_id, int lang_id)
{
    // code goes here...
}

I want to access it using PageMethods like: (Provided the condition that i have Enable PageMethods in ScriptManager):

<script type="text/javascript">
    $(document).ready(
        function () {
            $('#characterstics').html("loading");
            // '<%= product_id_property %>' , '<%= this.LanguageID %>'
            PageMethods.getCharacterstics(0 ,0 , OnSave);
        }
    );

    function OnSave(result) {
    }
</script>

I get the error "the http verb post to access path.. is not allowed"

I googled it and search the SO too but does not get any solution regarding to it Based on ASP.NET Routing.

What i believe is that because of asp.net routing the service methods are not accessible.

In addition i think i cannot even use JSON because of asp.net routing too.

Any help is appreciated.

Updated:

If i run the page with this url:

http://localhost:2606/searchdetail.aspx

The web method executed successfully.

Now

I have routing like this:

       routes.MapPageRoute("searchdetail", "searchdetail/{ID}", "~/searchdetail.aspx");
        routes.MapPageRoute("searchdetail", "searchdetail", "~/searchdetail.aspx");

The set_path() will work only for case 2 i.e without ID but does not work with case 1

if i try

   http://localhost:2606/searchdetail

It works fine

but if i try to use:

http://localhost:2606/searchdetail/123

It gives error that object expected.

So set_path() is the option what should i write.

like image 841
Moons Avatar asked Dec 13 '22 07:12

Moons


1 Answers

Currently, WebMethods don't work transparently with the Routing framework. There is a work around. You have to access the PageMethods directly by doing the following in your javascript:

PageMethods.set_path('/the/path/to/your/page.aspx');
PageMethods.YourMethod(params, onSuccess, onFailure);

I hope this helps.

like image 188
Neha Avatar answered Feb 22 '23 23:02

Neha