访问wcf-rest函数时出错 [英] Error while accessing wcf-rest function

查看:84
本文介绍了访问wcf-rest函数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试访问wcf-rest时出现以下错误。

合同''SelectorFront''中的操作''登录''指定方法''获取''在WebGetAttribute / WebInvokeAttribute上,但Method唯一允许的值是GET或POST。 ''System.ServiceModel.Description.WebScriptEnablingBehavior''不支持其他值。



我创建了一个wcf-rest, 1方法登录并有一个参数用户名

这是我的函数调用。

http:// localhost:2664 / FrontService.svc / Login?Username =最高



我的wcf如下



界面

I am getting following error when i try to access the wcf-rest.
Operation ''Login'' in contract ''SelectorFront'' specifies Method ''Get'' on the WebGetAttribute/WebInvokeAttribute, but the only allowed values for Method are GET or POST. Other values are not supported by ''System.ServiceModel.Description.WebScriptEnablingBehavior''.

I have created a wcf-rest, with 1 method "Login" and has one parameter "Username"
This is my function call.
http://localhost:2664/FrontService.svc/Login?Username=max

And my wcf is as following

Interface

[OperationContract]
[WebInvoke(Method = "Get", UriTemplate = "/Login/{UserName}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
string Login(string UserName);





服务





Service

public string Login(string UserName)
{
tblUser obj = (from m in dataContext.tblUsers
where m.UserName == UserName
select m).First();
JavaScriptSerializer oSerializer = new JavaScriptSerializer();
string sJSON = oSerializer.Serialize(obj);
return sJSON;
}







这个问题的解决方案是什么




what is the solution of this issue

推荐答案

我想这个小问题隐藏在你的Uri模板的场景背后。

所以,显然,你可以尝试以下一种格式声明你的方法: br $>


Hi , I suppose that this little issue hidden behind the scene of your Uri template.
So, obviously, you could try declare your method in next format:

[OperationContract]
[WebInvoke(Method = "Get", UriTemplate = "Login?UserName={UserName}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string Login(string UserName);





之后您的参数可能会成功路由到方法参数名为UserName。





And after that you parameter might be successfully routed to method parameter called UserName.

public string Login(string UserName)
{
tblUser obj = (from m in dataContext.tblUsers
where m.UserName == UserName
select m).First();
JavaScriptSerializer oSerializer = new JavaScriptSerializer();
string sJSON = oSerializer.Serialize(obj);
return sJSON;
} 


这篇关于访问wcf-rest函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆