简体   繁体   English

Web服务asp.net POST方法

[英]Web service asp.net POST method

Based on this tutorial ( http://www.codeproject.com/Articles/405189/How-to-access-SQL-database-from-an-iPhone-app-Via ) 基于本教程( http://www.codeproject.com/Articles/405189/How-to-access-SQL-database-from-an-iPhone-app-Via

I try to do a WebServices in asp.net. 我尝试在asp.net中做一个WebServices。 the GET method works... but the POST method don't. GET方法有效...但是POST方法无效。 (I verify with the app of chrome "POSTMAN") and have and error: Error 404, no found End not found (我使用Chrome“ POSTMAN”的应用程序进行了验证),并出现以下错误: 错误404,未找到结尾未找到

I hope u can help me with that, i'm started with web services. 我希望您能为我提供帮助,我从Web服务开始。

code ASP.net The Interface 代码ASP.net 接口

namespace JsonWcfService {
[ServiceContract]
public interface IGetEmployees
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "json/employees")]
    List<Employee> GetAllEmployeesMethod();
}
[ServiceContract]
public interface IPostEmployees
{
    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "json/InsertEmployee/{id1}/{id2}")]
    bool InsertEmployeeMethod(string id1, string id2);
}   

} }

The WFC 世界足协

namespace JsonWcfService
{
 public class GetEmployees : IGetEmployees 
 {
   public List<Employee> GetAllEmployeesMethod()
    {
         List<Employee> mylist = new List<Employee>();

         using (SqlConnection conn = new SqlConnection("data source=SQLREPORTES;database=prueba;persist security info=True;user id=myuser;password=mypassword;multipleactiveresultsets=True;"))
        {
            conn.Open();

            string cmdStr = String.Format("SELECT * FROM dbo.EmpDB");
            SqlCommand cmd = new SqlCommand(cmdStr, conn);
            SqlDataReader rd = cmd.ExecuteReader();

            if (rd.HasRows)
            {
                while (rd.Read())
                {
                    mylist.Add(new Employee(rd.GetString(0), rd.GetString(1))); //, rd.GetDecimal(2)));
                }
            }
            conn.Close();
        }
        return mylist;
    }
   //insert to DataBase
   public bool InsertEmployeeMethod(string id1, string id2)
   {
       int success = 0;
       using (SqlConnection conn = new SqlConnection("data source=SQLREPORTES;database=prueba;persist security info=True;user id=myuser;password=mypassword;multipleactiveresultsets=True;"))
       {
           conn.Open();

           //decimal value = Decimal.Parse(id3);
           string cmdStr = string.Format("INSERT INTO dbo.EmpDB VALUES('{0}','{1}')", id1, id2);
           SqlCommand cmd = new SqlCommand(cmdStr, conn);
           success = cmd.ExecuteNonQuery();

           conn.Close();
       }
       return (success != 0 ? true : false);
   }
   public bool UseHttpGet { get; set; }
}

[DataContract]
public class Employee
{
    [DataMember]
    public string firstname { get; set; }
    [DataMember]
    public string lastname { get; set; }
    [DataMember]
    public decimal salary { get; set; }
    public Employee(string first, string last)
    {
        firstname=first;
        lastname=last;
        //salary=sal;
    }
}

} }

And the web.config (Download Here ) 和web.config( 在此处下载)

<services>
  <service name="JsonWcfService.GetEmployees" behaviorConfiguration="EmpServiceBehaviour">
    <endpoint address ="" binding="webHttpBinding" contract="JsonWcfService.IGetEmployees" behaviorConfiguration="web">
    </endpoint>
  </service>
 </services>

Well in your Web.config you're using the contract IGetEmployees which doesn't have a definition for your InsertEmployee method thus you need to merge the two interfaces in one interface and modify your web.config instead of 在Web.config中,您正在使用合同IGetEmployees,该合同没有为InsertEmployee方法定义,因此您需要将两个接口合并为一个接口并修改web.config而不是

contract="JsonWcfService.IGetEmployees"

with contract="JsonWcfService.IEmployees" IEmployees define your two methods the get and post method contract="JsonWcfService.IEmployees" IEmployees定义get和post方法的两个方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM