我已经在我的vb.net 4.0Web应用程序中创建了一个脚本服务。不确定是哪一个--这有关系吗?)因为我想从客户端调用它。我收到客户端错误,说我的名称空间无法识别(下面代码中的HomepageService)。我尝试用项目中配置的名称"Root namespace“来限定它,但是js说它也不能识别这个名称空间。
这个应用程序很旧--我们最近把它从dotnet 2.1升级到了4.0。
我找到了下面的相关主题,因为当我试图在我的HomepageServices.asmx.vb中导入System.Web.Extensions时,visual studio说它无法识别它,即使我可以看到它,它就列在studio的References下。
无法解析System.Web.Extensions程序集
我试着发布了一个关于这个话题的后续问题,因为我试着遵循答案中的说明,但它们不起作用(我在项目>属性>应用程序中没有“目标框架”),但有人删除了它,因为我猜我不被允许问后续问题?
我已经查阅了各种网站,并遵循了说明,下面是一个示例:http://www.asp.net/ajax/documentation/live/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx
下面是位于my site根文件夹中的HomepageService.asmx内容:
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Collections.Generic
<System.Web.Services.WebService(Namespace:="http://localhost/appname")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
<System.Web.Script.Services.ScriptService()> _
Public Class HomepageService
Inherits System.Web.Services.WebService
Shared _rand As Random = New Random(Environment.TickCount)
<WebMethod()> _
Public Function Test(ByVal s As String) As Integer
Return _rand.Next(0, 120)
End Function
End Class来自母版页的代码片段:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="Scriptmanager1" runat="server" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="CallWebServiceMethods.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="HomepageService.asmx" />
</Services>
</asp:ScriptManager>在我的页面顶部,我导入了js:
PageRequestManager.js:
HomepageService.set_defaultSucceededCallback(
OnLookupComplete);
HomepageService.set_defaultFailedCallback(
OnError);
function OnLookup() {
HomepageService.Test(stb.value);
}
function OnLookupComplete(result, userContext) {
// userContext contains symbol passed into method
var res = document.getElementById("_resultLabel");
res.innerHTML = userContext + " : <b>" + result + "</b>";
}
function OnError(result) {
alert("Error: " + result.get_message());
}我的web.config一团糟,但我很乐意把它贴出来……
发布于 2012-01-27 23:36:05
终于让它工作了。这就是我是如何做到的。虽然不确定问题出在哪里..但遵循下面的过程确实起到了作用。
步骤1.使用新的网站项目,按照http://msdn.microsoft.com/en-us/library/bb532367(v=vs.90).aspx上的演练进行操作。
它起作用了。我将这称为模型项目。
步骤2.在我现有的web应用程序中创建文件,并复制模型项目中的所有代码:-在项目的根文件夹中创建WebService HelloWorld.asmx (studio还为我提供了一个嵌套文件Helloworld.asmx.vb)。这与模型项目不同,因为当我在该项目中创建webservice时,它将.asmx放在我的根文件夹中,但它将.asmx.vb放在App_Code文件夹中。不确定我是应该手动移动它还是其他什么?在过去,当我尝试在这个web应用程序中使用App_Code或App_data文件夹时,一切都像地狱一样……
步骤3.编辑webservice以使用我的web应用程序而不是网站:- Namespace Samples.Aspnet被注释掉,而不是替换为不同的名称空间,因为我的应用程序有一个默认的名称空间'Fubar',它与我的应用程序的项目名称相同。
以下是我的web应用根文件夹中HelloWorld.asmx.vb中的结果代码:
Imports System
Imports System.Web
Imports System.Collections
Imports System.Web.Services
Imports System.Web.Services.Protocols
'Namespace Samples.Aspnet
<WebService([Namespace]:="http://mycompany.org/"), _
WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1), _
System.Web.Script.Services.ScriptService()> _
Public Class HelloWorld
Inherits System.Web.Services.WebService
Public Sub New()
End Sub 'New
'Uncomment the following line if using designed components
'InitializeComponent();
<WebMethod()> _
Public Function Greetings() As String
Dim serverTime As String = _
String.Format("Current date and time: {0}.", DateTime.Now)
Dim greet As String = "Hello World. <br/>" + serverTime
Return greet
End Function 'Greetings
End Class 'HelloWorld
'End Namespace下面是HelloWorld.asmx中的代码,也在根文件夹中:
<%@ WebService Language="vb" CodeBehind="HelloWorld.asmx.vb" Class="Fubar.HelloWorld" %>步骤4.创建js "HelloWorld.js",将其粘贴到根文件夹中,就像在模型项目中一样,并粘贴模型项目中的代码。进行一次编辑以匹配我的应用程序的命名空间。以下是生成的代码:
var helloWorldProxy;
// Initializes global and proxy default variables.
function pageLoad() {
// Instantiate the service proxy.
// helloWorldProxy = new Samples.Aspnet.HelloWorld();
helloWorldProxy = new Fubar.HelloWorld();
// Set the default call back functions.
helloWorldProxy.set_defaultSucceededCallback(SucceededCallback);
helloWorldProxy.set_defaultFailedCallback(FailedCallback);
}
// Processes the button click and calls
// the service Greetings method.
function OnClickGreetings() {
var greetings = helloWorldProxy.Greetings();
}
// Callback function that
// processes the service return value.
function SucceededCallback(result) {
var RsltElem = document.getElementById("Results");
RsltElem.innerHTML = result;
}
// Callback function invoked when a call to
// the service methods fails.
function FailedCallback(error, userContext, methodName) {
if (error !== null) {
var RsltElem = document.getElementById("Results");
RsltElem.innerHTML = "An error occurred: " +
error.get_message();
}
}
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();步骤5.将代码添加到母版页中的ScriptManager标记中。这与模型项目在Default.aspx中的代码相对应;下面的ScriptManager标记与模型项目中的相同:
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager">
<Services>
<asp:ServiceReference path="~/HelloWorld.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="~/HelloWorld.js" />
</Scripts>
</asp:ScriptManager>步骤5.向我的测试页面添加控件,就像它们在模型项目中的Default.aspx中所做的那样:
<div id="divTestWebServices">
<button id="Button1" onclick="OnClickGreetings(); return false;">Greetings</button>
<div>
<span id="Results"></span>
</div>
</div>步骤6.运行Fubar并导航到测试页面,然后点击按钮。
它成功了!我不知道为什么它以前不能工作,但在这三天之后,我现在准备继续前进……
https://stackoverflow.com/questions/9024122
复制相似问题