从 Ajax 调用服务时出现 Web 服务错误“无法创建类型”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/888794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Web Service error 'Could not create type' when calling service from Ajax
提问by
I've downloaded the Ajax Control toolkit
我已经下载了 Ajax Control 工具包
When I create a website from the Ajax control Toolkit template, I can create a web service in a single file (asmx) and hook this up to different Ajax controls provided in the toolkit.
当我从 Ajax 控件工具包模板创建网站时,我可以在单个文件 (asmx) 中创建一个 Web 服务,并将其连接到工具包中提供的不同 Ajax 控件。
However if I add the Toolkit to an existing ASP.Net 2.0 appliction web project I cannot create web services in a single asmx file. If I create a web service VS always creates a code behind file as well. As a result of this calls to the web service (or loading the service directly in the browser) generate the following error:
但是,如果我将 Toolkit 添加到现有的 ASP.Net 2.0 应用程序 Web 项目,我将无法在单个 asmx 文件中创建 Web 服务。如果我创建一个 Web 服务 VS 总是会创建一个代码隐藏文件。由于对 Web 服务的调用(或直接在浏览器中加载服务)生成以下错误:
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not create type 'AddAjaxToApplication.List'.
Source Error:
Line 1: <%@ WebService Language="C#" CodeBehind="List.asmx.cs" Class="AddAjaxToApplication.List" %>
Does anyone know how to resolve this?
有谁知道如何解决这个问题?
Thanks,
谢谢,
Tim
蒂姆
回答by
I fixed this in the end.
我最终解决了这个问题。
In VS 2005 I just needed to right click on the web service and select "View Markup". I just changed the markup to use the correct namespace:
在 VS 2005 中,我只需要右键单击 Web 服务并选择“查看标记”。我只是更改了标记以使用正确的命名空间:
Line 1: <%@ WebService Language="C#" CodeBehind="List.asmx.cs" Class="List" %>
I Just removed the project name from the namespace to match the namespace used by List.asmx and this error went away.
我刚刚从命名空间中删除了项目名称以匹配 List.asmx 使用的命名空间,这个错误就消失了。
回答by Andy Joiner
I solved this by ensuring that MyService.dllwas deployed in the /binfolder below my Web.config.
我通过确保MyService.dll部署在/bin我的Web.config.
回答by Manik Khattar
Check in the Published folder if the path for the specified class files really exist or not, For me the App_Code folder was not present in the published directory so I manually placed the folder there and it is working perfectly :-)
如果指定的类文件的路径确实存在,请检查已发布文件夹,对我而言,已发布目录中不存在 App_Code 文件夹,因此我手动将该文件夹放在那里,并且它运行良好:-)
回答by atconway
In my instance I had renamed the class something different than the .asmx file, so when referencing the file from the .aspx code it would generate this error.
在我的实例中,我将类重命名为与 .asmx 文件不同的内容,因此当从 .aspx 代码引用文件时,它会生成此错误。
So I had 'AjaxManager.asmx', but had renamed the class to 'AjaxService', and the markup did not match. In my case, leaving the fully qualified namespace did not cause any issue. So if you change the class name (probably not reccomended anyway), make sure to correct the markup as well as shown below:
所以我有'AjaxManager.asmx',但已将类重命名为'AjaxService',并且标记不匹配。就我而言,离开完全限定的命名空间不会导致任何问题。因此,如果您更改类名(可能无论如何都不推荐),请确保更正标记以及如下所示:
<%@ WebService Language="vb" CodeBehind="AJAXManager.asmx.vb" Class="MyProject.UI.AjaxService" %>
回答by neeraj
One more Important Things and simple solution that i found out while creating web service . Try to avoid creating code behind file
我在创建 Web 服务时发现的另一件重要事情和简单的解决方案。尽量避免在文件后面创建代码
I was facing same problem while accessing web service in a virtual directory in iis 7 , then i created inline code asmx file and then access it , it was accessible
我在 iis 7 的虚拟目录中访问 web 服务时遇到了同样的问题,然后我创建了内联代码 asmx 文件,然后访问它,它是可以访问的
回答by hoang
I solved the problem by moving the code behind file MyService.asmx.csto the App_Codedirectory.
我通过将文件隐藏的代码移动MyService.asmx.cs到App_Code目录中解决了这个问题。
回答by Kokulan Eswaranathan
I solved this by giving the full path of the codebehind like this
我通过像这样提供代码隐藏的完整路径来解决这个问题
CodeBehind="~/codebehind.asmx.cs"
CodeBehind="~/codebehind.asmx.cs"

