ExtJs调用WCF ASP.NET和C#.NET不起作用 [英] ExtJs calls WCF ASP.NET AND C#.NET does not work
问题描述
我创建了一个新的"ASP.NET网站",其中包括Extjs javascript框架,但是我无法使ExtJ通过ajax调用WCF服务并正确返回结果.它总是显示失败消息(返回错误号500-Internal Server Error).
在我的测试网站项目中,它包含以下重要文件
1. Controller.svc及其位于App_Code文件夹中的Controller.cs后面的代码
2. Default.aspx
3. web.config
4.扩展库文件
请帮助我指出错误或错过的配置.
Controller.cs文件中的代码.
I have created a new "ASP.NET Web Site" which included Extjs javascript framework but I cannot make the ExtJs to call WCF Service via ajax and return result properly. It always shows failure message (return with error number 500 - Internal Server Error).
In my testing website project, it contains the following important files
1. Controller.svc and its codebehind Controller.cs which is in App_Code folder
2. Default.aspx
3. web.config
4. Ext lib files
Please help me to point what is wrong or what configuration I missed.
Codes in Controller.cs file.
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Diagnostics;
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Controller
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/Add")]
public int Add(int a, int b)
{
Debug.WriteLine("Adding....");
return a + b;
}
}
Default.aspx文件中的代码.
Codes in Default.aspx file.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
<link rel="stylesheet" type="text/css" href="scripts/ext-3.2.1/resources/css/ext-all.css" />
<!-- LIBS -->
<script type="text/javascript" src="scripts/ext-3.2.1/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="scripts/ext-3.2.1/ext-all.js"></script>
<script type="text/javascript" language="javascript">
Ext.onReady(function() {
var aVal = 5;
var bVal = 7;
var params = { a: aVal, b: bVal };
Ext.lib.Ajax.defaultPostHeader = 'application/json';
Ext.Ajax.request({
url: 'Controller.svc/Add',
method: 'POST',
params: Ext.encode(params),
success: function(response, options) {
alert("sucess: " + response.responseText);
Ext.MessageBox.alert('sucess', 'Succeed to add values')
},
failure: function(response, options) {
alert("failure: " + response.responseText);
Ext.MessageBox.alert('Failed', 'Unable to add values');
}
});
}); // End of Ext.onReady
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
web.config文件中的代码:
Codes in web.config file:
<configuration>
.
.
.
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ControllerAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="Controller">
<endpoint address="" behaviorConfiguration="ControllerAspNetAjaxBehavior"
binding="webHttpBinding" contract="Controller" />
</service>
</services>
</system.serviceModel>
</configuration>
推荐答案
花了几天的时间研究之后,我现在可以使其工作.我想分享我的解决方案,以防万一有人遇到像我这样的问题;)
Controller.cs中的代码必须类似于以下
After taking a few days to investigate I can make it work now. I would like to share my solution just in case anyone having a problem like me ;)
Codes in Controller.cs must be like below
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Diagnostics;
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Controller
{
[OperationContract]
[WebInvoke(Method = "POST", // Even though the default is POST but I think there is a bug that sometimes make it not working so it would be better to specify
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public int Add(int a, int b)
{
Debug.WriteLine("Adding....");
return a + b;
}
}
Default.aspx中没有任何变化
而且Web.config中必须有一些特定的内容,如下所示
There is nothing changed in Default.aspx
And There is something must be specific in Web.config as below
<configuration>
.
.
.
<system.serviceModel>
<behaviors>
<!-- string of adding new section -->
<serviceBehaviors>
<behavior name="WebScriptBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<!-- end of adding new section -->
<endpointBehaviors>
<behavior name="WebScriptBehavior">
<webHttp /> <!-- change from enableWebScript /-->
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="Controller">
<endpoint address="" contract="Controller"
behaviorConfiguration="WebScriptBehavior"
binding="webHttpBinding"/>
</service>
</services>
</system.serviceModel>
</configuration>
这篇关于ExtJs调用WCF ASP.NET和C#.NET不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!