我正在尝试从url调用wcf rest服务但是它显示的错误如下错误: 最可能的原因: •地址中可能存在输入错误。 •如果您点击某个链接,则该链接可能已过期。
我需要针对移动应用的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 :(得分:0)
使用WebGet而不是WebInvoke。 还要添加json端点。