Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...