“webmethod”类型或命名空间名称未找到。

10

我正在使用jquery、ajax和.net调用一个方法。我在网上看到很多示例都是说要在方法上方放置[Webmethod],但我一直收到错误,即找不到类型或命名空间名称“webmethod”。 我已经在顶部放置了 "using System.Web.Services;",还需要做什么吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

public partial class _Default : System.Web.UI.Page
{

[WebMethod]
public static string GetDate()
{
    return DateTime.Now.ToString();
}
}
3个回答

16
在你的项目中添加对 System.Web.Services.dll 的引用。
如果你已经有了正确的 using 语句,那么你收到这个错误信息很可能是因为缺少该引用。

7
我理解你的意思是“只需编写 Using System.Web.Services; 就可以完美运行,谢谢。” - Anicho

2
请在页面顶部添加以下内容:
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

1

回答这个问题的简单方法是从 .net framework 文件夹中添加 system.web.services 的引用。

例子:

假设我有一个已经引用了 system.web.services 的项目。

现在,如果我右键单击 System.web.services ,你会发现这个程序集位于 .Net 路径中,因此您可以轻松地从那里向项目添加对该程序集的引用。

enter image description here

解决方案

只需右键单击“引用”,选择“添加引用”,点击选择管理器窗口的浏览器按钮,进入路径并添加引用即可。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接