c# - 如果您实现了 ASP.NET 路由,ASP.NET 中的 PageMethods 将无法工作

标签 c# asp.net json url-routing scriptmanager

我有一个网络方法:

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

我想使用 PageMethods 访问它,例如: (前提是我在 ScriptManager 中启用了 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 动词发布.. 是不允许的”

我用谷歌搜索并搜索了 SO,但没有得到任何基于 ASP.NET 路由的解决方案。

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

此外,我认为我什至无法使用 JSON,因为还有 asp.net 路由。

感谢任何帮助。

更新:

如果我使用这个 url 运行页面:

http://localhost:2606/searchdetail.aspx

网络方法执行成功。

现在

我有这样的路由:

       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() 是我应该写的选项。

最佳答案

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

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

希望对您有所帮助。

关于c# - 如果您实现了 ASP.NET 路由,ASP.NET 中的 PageMethods 将无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8147186/

相关文章:

c# - 在 Autofac 中注册时如何避免使用 new 两次

c# - .NET/C# 中是否内置了用于在对象之间复制值的内容?

C# HttpClient - 我可以强制关闭连接吗?

c# - 在 Java 服务器和 C# 客户端之间共享数据对象的最快方法

c# - 无法在 ASP.Net MVC 5 中传递日期时间

asp.net - MVC 4 Web API 区域 404 错误 : "The controller for path '/Jobs/Send' was not found or does not implement IController."

JSON 与 HTML Ajax 响应

asp.net - 在 IIS 7.5 下调试时生成的程序集与源不匹配的问题

ios - 谷歌地图上未显示注释或标记

mysql - 如何使用 Passport 向浏览器发送 json 响应