【问题标题】:aspx web method returning a blank page?aspx web 方法返回一个空白页?
【发布时间】:2013-10-31 15:54:30
【问题描述】:

我在一个 aspx 页面中有一个 WebMethod,它似乎只返回一个空白页面,我认为它甚至没有被调用,可能是什么原因造成的?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.Script.Services;

namespace Test.webservices.mainGrid
{
    public partial class getMainGrid : System.Web.UI.Page
    {

        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Xml)]
        public static string GetRecords()
        {
      return "test";

        }
    }
    }

【问题讨论】:

  • 解决方案是什么?

标签: c# asp.net web-services


【解决方案1】:

这是一个 ASP.NET AJAX 页面方法,它可以被 JavaScript 调用(例如 ASP.NET AJAX 或 jQuery)。文本test 不渲染,因为页面生命周期不知道用这个静态方法做什么;由于您的页面上没有标记或呈现给页面的逻辑,因此您的页面在运行时是空白的。

由于该方法是静态的,它不是实际页面实例的一部分,因此不能由页面类本身的代码调用。为此,当您从脚本调用它时,您将无法访问页面上的其他控件。 ASP.NET AJAX 页面方法对于从服务器获取数据以使用客户端非常有用。

要了解有关 ASP.NET AJAX 页面方法的更多信息,请阅读Using jQuery to directly call ASP.NET AJAX page methods

在过去的几年中,我在我的 ASP.NET WebForms 项目中广泛使用了 ASP.NET AJAX 页面方法。它们非常适合从服务器获取数据以更新您的用户界面。

【讨论】:

  • 嗨,是的,我正在尝试使用 jQuery(从另一个页面)调用它。我也在浏览器中尝试过,我只是得到一个空白页面。这只能从自身调用吗?
  • @realtek - 不,您可以从另一个页面调用页面方法,但您必须在 URL 中指定页面名称和静态方法名称(即YourPageName.aspx/YourPageMethodName)。发布您尝试使用的客户端代码,我可能会提供更多帮助。 :-)
  • @realtek - 你是不是故意让页面方法返回 XML 而不是默认的 JSON?
猜你喜欢
  • 1970-01-01
  • 2011-02-01
  • 1970-01-01
  • 2020-05-23
  • 2016-03-31
  • 2019-12-20
  • 1970-01-01
  • 2019-12-08
  • 1970-01-01
相关资源
最近更新 更多