我有一个网络方法:
[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/