REST Web服务无法正常工作,

时间:2017-12-22 18:06:59

标签: c# asp.net rest web-services

您好,我分享了我的代码,请告诉我为什么我的代码不起作用。我对这个REST Web服务很新,我刚开始编写这个。所以给下面提到的代码提供一些解决方案。

这是我的webservices.cs代码

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

namespace webservice_PCCFWL
{

    [ServiceContract]
    public interface IWebServices
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "/get_userauthentication/{username}/{password}",Method ="GET",
                      RequestFormat = WebMessageFormat.Json,
                       ResponseFormat = WebMessageFormat.Json
        )]
        List<userloginretrive> getuserdetails(string username,string password);
    }

    [DataContract]
    public class userloginretrive
    {
        public userloginretrive()
        {
            uname = null;
            udesignation = null;
            division = null;
            ranger = null;
        }
        [DataMember(Name = "uname", Order = 1)]
        public string uname { get; set; }
        [DataMember(Name = "udesignation", Order = 2)]
        public string udesignation { get; set; }
        [DataMember(Name = "division", Order = 3)]
        public string division { get; set; }
        [DataMember(Name = "ranger", Order = 4)]
        public string ranger { get; set; }
    }
}

=========================

这是我的.cs代码

using System;
using System.Collections.Generic;
using System.Web.Security;
using System.Security.Cryptography;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace webservice_PCCFWL
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "WebServices" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select WebServices.svc or WebServices.svc.cs at the Solution Explorer and start debugging.
    public class WebServices : IWebServices
    {
        string myKey;
        TripleDESCryptoServiceProvider cryptDES3 = new TripleDESCryptoServiceProvider();
        MD5CryptoServiceProvider cryptMD5Hash = new MD5CryptoServiceProvider();
        public WebServices()
        {
            myKey = "kt763g-kj_7gfhd7GJD-563bjf";
        }
        private string EncryptUser(string myString)
        {
            cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey));
            cryptDES3.Mode = CipherMode.ECB;
            ICryptoTransform desdencrypt = cryptDES3.CreateEncryptor();
            var MyASCIIEncoding = new ASCIIEncoding();
            byte[] buff = ASCIIEncoding.ASCII.GetBytes(myString);
            return Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
        }
        public List<userloginretrive> getuserdetails(string username,string password)
        {
            List<userloginretrive> objlist = new List<userloginretrive>();
            string pass = EncryptUser(password);
            try
            {
                SqlParameter[] para = new SqlParameter[]
                {
                    new SqlParameter("@p_login_id",username),
                    new SqlParameter("@p_password",pass)
                };

                using (DataTable dt = SQLHelper.DataAcessLayer.SqlHelper.ExecuteDataTable(sql.Connection.Configuration.ConnectionString, CommandType.Text, "select User_Name,User_Designation,Wl_Division,Wl_Range from UserDetails where User_LoginId=@p_login_id and User_Password=@p_password", para))
                {
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            objlist.Add(new userloginretrive
                            {
                                uname = Convert.ToString(dr["User_Name"]),
                                udesignation = Convert.ToString(dr["User_Designation"]),
                                division = Convert.ToString(dr["Wl_Division"]),
                                ranger = Convert.ToString(dr["Wl_Range"])
                            });
                        }
                    }
                    else
                    {
                        objlist.Add(new userloginretrive
                        {
                            uname = "no",
                            udesignation = "no",
                            division = "no",
                            ranger = "no"
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return objlist;
        }
    }
}

======================

这是我的webconfig文件

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
    <add key="myConnectionString"
      value="Data Source=localhost;Initial Catalog=PCCFWL;uid=sa;pwd=sparc_123;Pooling=false;MINPOOLSIZE=20;MAXPOOLSIZE=1000;"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
  <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
    </httpModules>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehaviors">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
    <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="serviceBehaviors" name="webservice_PCCFWL.WebServices">
        <endpoint address="WebServices.svc" contract="webservice_PCCFWL.IWebServices" binding="webHttpBinding" behaviorConfiguration="web"></endpoint>
        <endpoint address="" contract="IMetadataExchange" binding="mexHttpBinding"></endpoint>
      </service>
    </services>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="ApplicationInsightsWebTracking"/>
      <remove name="TelemetryCorrelationHttpModule"/>
      <add name="TelemetryCorrelationHttpModule"
        type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation"
        preCondition="integratedMode,managedHandler"/>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
        preCondition="managedHandler"/>
    </modules>    
    <!--
        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"/>
    <validation validateIntegratedModeConfiguration="false"/>
  </system.webServer>

</configuration>

请给出一些解决方案。我的程序正在运行唯一的第一页服务,然后什么也没发生。所以请在这里帮助我。

0 个答案:

没有答案
相关问题