.NET ASMX-返回纯JSON?
我快发疯了。我已经查看了以下条目,其中没有一个纠正了我看到的异常行为:.NET ASMX-返回纯JSON?,.net,wcf,web-services,json,asmx,.net,Wcf,Web Services,Json,Asmx,我快发疯了。我已经查看了以下条目,其中没有一个纠正了我看到的异常行为: 我还查看并确认了我的设置: 这是我的代码(ASMX代码隐藏): 类别: web.config 现在我得到了这个错误: IIS指定的身份验证方案“IntegratedWindowsAuthentication,Anonymous”,但绑定仅支持指定一个身份验证方案。有效的身份验证方案有摘要、协商、NTLM、基本或匿名。更改IIS设置,以便只使用单个身份验证方案。 我该怎么处理 为什么不将ASMX Web服务迁移到
现在我得到了这个错误:
IIS指定的身份验证方案“IntegratedWindowsAuthentication,Anonymous”,但绑定仅支持指定一个身份验证方案。有效的身份验证方案有摘要、协商、NTLM、基本或匿名。更改IIS设置,以便只使用单个身份验证方案。
我该怎么处理 为什么不将ASMX Web服务迁移到WCF? NET Framework 3.5中的WCF API本机支持JSON web服务
此外,微软宣布ASMX为“遗留技术”,并建议“现在使用Windows通信基础(WCF)创建Web服务和XMLWeb服务客户端”。()
您可能希望查看以下链接以开始:using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace MyFirstWCF
{
[ServiceContract]
public interface IContract
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/CustomerName/{CustomerID}")]
string GET_CustomerName(string CustomerID);
}
}
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
namespace MyFirstWCF
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.NotAllowed)]
public class Service : IContract
{
public string GET_CustomerName(string CustomerID)
{
return "Customer Name: " + CustomerID;
}
}
}
Service.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace MyFirstWCF
{
[ServiceContract]
public interface IContract
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/CustomerName/{CustomerID}")]
string GET_CustomerName(string CustomerID);
}
}
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
namespace MyFirstWCF
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.NotAllowed)]
public class Service : IContract
{
public string GET_CustomerName(string CustomerID)
{
return "Customer Name: " + CustomerID;
}
}
}
WCFHost.cs(控制台应用程序)
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="MyFirstWCF.Service">
<endpoint address="http://127.0.0.1:8000/api"
binding="webHttpBinding"
contract="MyFirstWCF.IContract" />
</service>
</services>
</system.serviceModel>
</configuration>
对于要编译的示例项目,需要添加对System.RuntimeSerialization
、System.ServiceModel
和System.ServiceModel.Web
的.NET引用。对该方法的请求设置的“内容类型”是什么
根据我对ASP.NET所做的操作,如果将其设置为
text/xml
,您将返回xml;但是,如果设置为application/json
,您将返回json。您可以发布用于调用服务的代码吗?Keith,关于您的最新更新,为什么不尝试自行托管WCF应用程序以进行测试?它将把IIS从等式中去掉。使用控制台应用程序自托管非常简单。(在我的回答中检查WCFHost.cs)。通过删除集成安全性(我们无论如何都没有使用)修复了IIS问题。现在SVC上出现500错误。--从自托管到web托管的转换不是什么大问题吗?我们有管理层希望在上周启动并运行。“应该只需要10分钟”。也许我现在要“了解”这个问题了。丹尼尔·瓦萨洛——我只是想对你的耐心、戳戳和戳戳说一声非常感谢。似乎ASMX或WCF w/JSON在任何地方都没有明确的文档记录。我发现了太多不起作用的东西,我现在可能有冲突的类/方法修饰符。{sigh/}Keith,我相信很多人都会理解您最初为了解WCF API的大局所做的努力。WCF看起来很复杂,因为它很大。此外,最近添加了JSON(使用.NET3.5)。。。我真正建议的是从一个非常小的项目开始,最好是在控制台应用程序中自行托管。然后在它的基础上建立。。。最后一个建议是:如果你愿意投资69美元,网站上有一个非常有价值的10小时视频教程。它将从最基本的开始,一切都解释得非常清楚。强烈推荐。嗯。我的机器上好像有什么东西。我没有创建AJAX WFC服务的选项。有什么东西我应该安装在VS2008之上吗?你可以创建一个普通的WCF服务;你可以用属性来修饰你的接口,以表明响应格式将是JSON。我没有创建任何WCF服务的选项。是否可以尝试创建新的解决方案,以检查是否显示WCF选项?添加新项目时,应该可以选择框架版本,如下所示:。确保在出现提示时选择了.NET 3.5。确定。按照这条路线,我确实可以选择WCF项目。关于构建支持.NET WCF JSON的WS并从单独的(非.NET)网站调用它的任何参考资料?可能是一个纯HTML站点,调用方法是JS?JS中的application/json。注意:JS不在我们的页面上!WS需要响应一个外部JS请求。我认为关键是它将以请求的类型返回响应,在您的示例中,它似乎是“text/xml”。即使它不是来自您编写的东西的请求,您也可以轻松地编写一个自己的小JS脚本,用“application/json”测试它。内容类型非常重要。谢谢
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name ="soapBinding">
<security mode="None" />
</binding>
</basicHttpBinding>
<webHttpBinding>
<binding name="webBinding">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="poxBehavior">
<webHttp/>
</behavior>
<behavior name="jsonBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="defaultBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="RivWorks.Web.Services.Negotiater" behaviorConfiguration="defaultBehavior">
<endpoint address="json"
binding="webHttpBinding"
bindingConfiguration="webBinding"
behaviorConfiguration="jsonBehavior"
contract="RivWorks.Web.Services.INegotiaterJSON" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
<baseAddressPrefixFilters>
<add prefix="http://dev.rivworks.com" />
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
</system.serviceModel>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function sendReq() {
alert("Before AJAX call");
$.ajax(
{
type: "POST"
, url: "http://dev.rivworks.com/Services/Negotiater.svc/GetSetup"
, data: "{ \"ref\":\"http://www.rivworks.com/page.htm\", \"dt\":\"Mon Dec 14 2009 10:45:25 GMT-0700 (MST)\", \"productId\":\"5fea7947-251d-4779-85b7-36796edfe7a3\" }"
, contentType: "application/json; charset=utf-8"
, dataType: "json"
, success: GetMessagesBack
, error: Failure
}
);
alert("After AJAX call");
}
function GetMessagesBack(data, textStatus) {
alert(textStatus + "\n" + data);
}
function Failure(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus + "\n" + errorThrown + "\n" + XMLHttpRequest);
}
</script>
</head>
<body>
<div id="test">Bleh</div>
<!--<button onclick="javascript:sendReq()">TEST IT</button>-->
<a href="javascript:sendReq()">Test it</a>
</body>
</html>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace MyFirstWCF
{
[ServiceContract]
public interface IContract
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/CustomerName/{CustomerID}")]
string GET_CustomerName(string CustomerID);
}
}
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
namespace MyFirstWCF
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.NotAllowed)]
public class Service : IContract
{
public string GET_CustomerName(string CustomerID)
{
return "Customer Name: " + CustomerID;
}
}
}
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
using System.Threading;
using System.Text;
namespace MyFirstWCF
{
class Program
{
private static WebServiceHost M_HostWeb = null;
static void Main(string[] args)
{
M_HostWeb = new WebServiceHost(typeof(MyFirstWCF.Service));
M_HostWeb.Open();
Console.WriteLine("HOST OPEN");
Console.ReadKey();
M_HostWeb.Close();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="MyFirstWCF.Service">
<endpoint address="http://127.0.0.1:8000/api"
binding="webHttpBinding"
contract="MyFirstWCF.IContract" />
</service>
</services>
</system.serviceModel>
</configuration>
[DataContract]
public class POSITION
{
[DataMember]
public int AssetID { get; set; }
[DataMember]
public decimal Latitude { get; set; }
[DataMember]
public decimal Longitude { get; set; }
}