小编典典

如果已实现ASP.NET路由,则ASP.NET中的PageMethods将无法工作

json

我有一个网络方法:

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

我想使用PageMethods来访问它:(提供了在ScriptManager中启用Enable PageMethods的条件):

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

    function OnSave(result) {
    }
</script>

我收到错误消息“ http动词后无法访问路径..”

我在Google上搜索并搜索了SO,但是没有基于ASP.NET Routing的任何解决方案。

我认为是由于asp.net路由服务方法不可访问。

另外我认为我也不能使用JSON,因为asp.net路由也是如此。

任何帮助表示赞赏。

更新:

如果我使用此网址运行页面:

http://localhost:2606/searchdetail.aspx

Web方法成功执行。

现在

我有这样的路由:

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

set_path()仅适用于情况2,即没有ID,但不适用于情况1

如果我尝试

   http://localhost:2606/searchdetail

工作正常

但是,如果我尝试使用:

http://localhost:2606/searchdetail/123

它给出该对象预期的错误。

所以set_path()是我应该写的选项。


阅读 250

收藏
2020-07-27

共1个答案

小编典典

当前,WebMethods不能与路由框架透明地工作。有一个解决方法。您必须通过在JavaScript中执行以下操作直接访问PageMethods:

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

我希望这有帮助。

2020-07-27