[英]HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.
I am working on WCF REST Service and i am facing the above mentioned problem. 我正在使用WCF REST服务,并且遇到上述问题。 I am new to WCF REST Service. 我是WCF REST服务的新手。 Please Help me out of this problem. 请帮我解决这个问题。 My Code is shown below. 我的代码如下所示。
IService1.cs IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace WCFAJ
{
[ServiceContract]
public interface IService1
{
[OperationContract]
//[WebGet ( UriTemplate = "GET", ResponseFormat = WebMessageFormat.Json) ]
List<User> Get();
[OperationContract]
[WebGet ( UriTemplate = "GET/{id}", ResponseFormat = WebMessageFormat.Json )]
User GetById(int id);
[OperationContract]
[WebInvoke(Method="POST", UriTemplate = "POST", RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
void Post(User user);
[OperationContract]
[WebInvoke(Method="PUT", UriTemplate = "PUT", RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
void Put( User user);
[OperationContract]
[WebInvoke(Method="DELETE", UriTemplate = "DELETE/{id}", RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
void Delete(int id);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<User> Users { get; set; }
}
public class User
{
public int userid { get; set; }
[Required(ErrorMessage = "Username is required")]
public string Username { get; set; }
public string Fname { get; set; }
public string Lname { get; set; }
public string Address { get; set; }
public string Email { get; set; }
[StringLength(10, MinimumLength = 10, ErrorMessage = "Phone Number should be 10 characters long")]
public string Phone { get; set; }
}
//public class User
//{
// bool boolValue = true;
// string stringValue = "Hello ";
// [DataMember]
// public bool BoolValue
// {
// get { return boolValue; }
// set { boolValue = value; }
// }
// [DataMember]
// public string StringValue
// {
// get { return stringValue; }
// set { stringValue = value; }
// }
//}
}
Service1.svc.cs 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;
using System.Linq;
namespace WCFAJ
{
public class Service1 : IService1
{
List<User> user;
List<User> IService1.Get()
{
UsersContext db = new UsersContext();
user = db.Users.ToList();
return user;
}
User IService1.GetById(int id)
{
UsersContext db = new UsersContext();
User get;
get = db.Users.Where(i => i.userid == id).Single();
return get;
}
void IService1.Post(User uSer)
{
UsersContext db = new UsersContext();
db.Users.Add(uSer);
}
void IService1.Put(User useR)
{
UsersContext db = new UsersContext();
User user1 = db.Users.Where(i => i.userid == useR.userid).Single();
user1.Username = useR.Username;
user1.Fname = useR.Fname;
user1.Lname = useR.Lname;
user1.Email = useR.Email;
user1.Address = useR.Address;
user1.Phone = useR.Phone;
db.SaveChanges();
}
void IService1.Delete(int id)
{
UsersContext db = new UsersContext();
User x = db.Users.Where(i => i.userid == id).Single();
db.Users.Remove(x);
db.SaveChanges();
}
}
}
Web.Config Web.Config中
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=DEL1-DHP-28358;Initial Catalog=WCF;User ID=sa;Password=Global@123" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="WebHttpBinding">
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"></transport>
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="WCFAJ.Service1" behaviorConfiguration="WCFAJ.Service1Behavior" >
<endpoint binding="webHttpBinding" bindingConfiguration="WebHttpBinding" behaviorConfiguration="webBehavior" contract="WCFAJ.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:55610/Service1.svc/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFAJ.Service1Behavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpsGetEnabled="false" />
<!-- 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="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp automaticFormatSelectionEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" >
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory" relativeAddress="Service1.svc" service="WCFAJ.Service1"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Your endpoint isn't a REST endpoint. 您的端点不是REST端点。 REST endpoints are defined with a specific binding (webHttpBinding) and behavior (webHttp). REST端点使用特定的绑定(webHttpBinding)和行为(webHttp)进行定义。 Add to WCFAJ.Service1
, in addition to what you have, endpoint
for REST, like: 除了所拥有的内容外,还将REST的endpoint
添加到WCFAJ.Service1
中,例如:
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="REST" contract="WCFAJ.IService1"/>
and to <endpointBehaviors>
: 并<endpointBehaviors>
:
<behavior name="REST">
<webHttp/>
</behavior>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.