Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

c# - Not Getting the Data in URL by Web Service

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; }
}

}

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"
            };
        }
        }
       }
    }

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

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you just forgot few things in your web.config :

<endpointBehaviors>
    <behavior>
        <webHttp helpEnabled="true"/>
    </behavior>
</endpointBehaviors>

And

<protocolMapping>
   <add binding="webHttpBinding" scheme="http" />
</protocolMapping>

If you don't put all this stuffs in your web.config you won't be able to get your services working.

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>

For more details I had written few months ago a post on my blog about WCF and REST :

Simple WCF and REST service

WCF and POST method


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...