不通过 Web 服务获取 URL 中的数据 [英] Not Getting the Data in URL by Web Service
问题描述
我正在制作网络服务而不是通过 URL 获取解析数据WEB SERVICE的代码是这个.我的 IService 类
I am making web service and not getting the parse data through URL Code of WEB SERVICE is this. my IService class
namespace DataService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedResponse)]
List<RequestData> GetUser(RequestData data);
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "UsersList/{id}", RequestFormat = WebMessageFormat.Json)]
RequestData UsersList(string id);
}
[DataContract]
public class RequestData
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public string Address { get; set; }
}
}
这是我的service1类继承的Iservice1类
this is my service1 class inherited by Iservice1 Class
namespace DataService
{
public class Service1 : IService1
{
public List<RequestData> GetUser(RequestData data)
{
List<RequestData> list = new List<RequestData>();
if (data.Name.ToUpper() == "MAIRAJ")
{
list.Add(new RequestData
{
Name = "Mairaj",
Age = 25,
Address = "Test Address"
});
list.Add(new RequestData
{
Name = "Ahmad",
Age = 25,
Address = "Test Address"
});
list.Add(new RequestData
{
Name = "Minhas",
Age = 25,
Address = "Test Address"
});
}
return list;
}
public RequestData UsersList(string userId)
{
if (userId == "1")
{
return new RequestData
{
Name = "Mairaj",
Age = 25,
Address = "Test Address"
};
}
else
{
return new RequestData
{
Name = "Amir",
Age = 25,
Address = "Test Address"
};
}
}
}
}
我在部署 Web 服务后提供此 URL http://116.58.61.180/ADG/Service1.svc应该解析什么确切的 url 来获取数据
I am giving this URL after deploying web service http://116.58.61.180/ADG/Service1.svc what Exact url should parse to get the data
这是我的 web.config
this is my web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
推荐答案
我想你只是在 web.config 中忘记了一些东西:
I think you just forgot few things in your web.config :
<endpointBehaviors>
<behavior>
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
和
<protocolMapping>
<add binding="webHttpBinding" scheme="http" />
</protocolMapping>
如果您不将所有这些内容都放在 web.config 中,您将无法让您的服务正常工作.
If you don't put all this stuffs in your web.config you won't be able to get your services working.
完整的 Web.config 应该是这样的:
The full Web.config would be like this :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
要了解更多详细信息,我几个月前在我的博客上写了一篇关于 WCF 和 REST 的文章:
For more details I had written few months ago a post on my blog about WCF and REST :
这篇关于不通过 Web 服务获取 URL 中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!