2013-10-27 50 views
0

可能存在输入错误,它试图从url调用wcf rest服务,但显示如下错误:错误: 最有可能的原因: •可能有打字错误地址。 •如果您点击链接,它可能会过期。wcf rest service 400错误:地址

我需要JSON性反应对于移动应用

这里我的代码:

Iservice.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 


namespace SampleRestSample 
{ 
    interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface IService1 
    { 

     [OperationContract] 
     [WebInvoke(Method = "GET", UriTemplate = "Book/{id}", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] 
     List<Prasad> GetBookById(string id); 
    } 
    [DataContract] 
    public class Prasad 
    { 
     [DataMember] 
     public string Name { get; set; } 
     [DataMember] 
     public string Age { get; set; } 
    } 


} 

Service1.svc.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

namespace LoginRestSample 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    public class Service1 : SampleRestSample 
    { 
     List<Prasad> list = new List<Prasad>(); 
     public List<Prasad> GetBookById(string id) 
     { 
      try 
      { 
       Prasad cls = new Prasad(); 
       cls.Age = "24"; 
       cls.Name = "prasad"; 
       list.Add(cls); 

       //int bookId = Convert.ToInt32(id); 

       //using (SampleDbEntities entities = new SampleDbEntities()) 
       //{ 
       // return entities.Books.SingleOrDefault(book => book.ID == bookId); 
       //} 
      } 
      catch 
      { 
       throw new FaultException("Something went wrong"); 
      } 
      return list; 
     } 
    } 
} 

的web.config

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0"> 
     <assemblies> 
     <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
     </assemblies> 
    </compilation> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="WcfRestSample.SampleRestSample"> 
     <endpoint address="" behaviorConfiguration="restfulBehavior" 
      binding="webHttpBinding" bindingConfiguration="" contract="WcfRestSample.ISampleRestSample" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost/SampleRestSample" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="restfulBehavior"> 
      <webHttp automaticFormatSelectionEnabled="true" /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name=""> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    </system.webServer> 

</configuration> 

任何解决方案

谢谢您提前。

回答

0

使用WebGet而不是WebInvoke。 也添加json端点。