如何在aspx页面的服务器脚本块中使用[WebMethod]? [英] How to use a [WebMethod] in server script blocks in the aspx page?

查看:157
本文介绍了如何在aspx页面的服务器脚本块中使用[WebMethod]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的.Net 3.5页面,该页面具有一些HTML和 WebMethod 。当我尝试从浏览器调用 WebMethod 时,它会不断重播页面。任何想法如何使它工作?



这是我的代码:

 <%@页面语言= C# Inherits = CFW.WebUI.Page%> 
<!DOCTYPE html PUBLIC-// W3C // DTD XHTML 1.0 Transitional // EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional。 dtd>
< script runat = server>

[System.Web.Services.WebMethod]
公共静态字符串Test()
{
return Hola!;
}

< / script>
< html xmlns = http://www.w3.org/1999/xhtml>
...
< / html>

注意:我不是使用Ajax.Net 。我只想从客户端(使用jQuery)调用WebMethod。

解决方案

我不确定如果没有 ScriptManager ,但是绝对可以在不使用 PageMethod PageMethod c $ c>关键字。您可以像调用其他任何Web服务一样调用ASP页面。 yourpage.aspx / MethodName 。该函数应使用 [WebMethod] 属性修饰,无需放置 [ScriptMethod] 标记或页面类本身上的任何内容。我确实注意到我 POST 数据到这些函数,而不是 GET ,但是我不确定是否需要。 / p>

I'm trying to create a simple .Net 3.5 page thas has some HTML and a WebMethod. When I try to call my WebMethod from my browser, but it keeps returing the page. Any ideas how to get it to work?

This is my code:

<%@ Page Language="C#" Inherits="CFW.WebUI.Page" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    [System.Web.Services.WebMethod]
    public static string Test()
    {
        return "Hola!";
    }

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
...
</html>

Note: I'm not using Ajax.Net. I just want to call the WebMethod from the client (using jQuery).

解决方案

I'm not sure if this works without the ScriptManager, but it is definately possible to call a PageMethod without using the PageMethod keyword. You can call your ASP page just like any other webservice, eg. yourpage.aspx/MethodName. The function should be decorated with a [WebMethod] attribute, there is no need to put the [ScriptMethod] tag or anything on the page class itself. I do notice I POST data to these functions rather than GET, but I am not sure if this is required.

这篇关于如何在aspx页面的服务器脚本块中使用[WebMethod]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆