웹 API 컨트롤러 메서드에 의해 호출되는 데이터 액세스 레이어가있는 웹 API 2입니다. 캐스팅 오류

사용자 3020047

나는 얻는다 :

'System.Net.Http.HttpResponseMessage'형식의 개체를 'System.Web.Http.IHttpActionResult 형식으로 캐스팅 할 수 없습니다.

웹 API2 컨트롤러 메서드는 int를 반환하는 데이터 액세스 계층을 호출합니다. 캐스트에서 실패합니다.

return (IHttpActionResult)httpResponse;

암호:

[HttpGet]
[Route("registeruser/{currentDateTime}/{userName}/{userPassword}/{ipAddress}/")]
public IHttpActionResult RegisterUser(DateTime currentDateTime, string userName, string userPassword, string ipAddress)
{
        try
        {
            HttpResponseMessage httpResponse;

            int returnValue = 0;

            returnValue = dataaccesslayer.RegisterUser(currentDateTime, userName, userPassword, ipAddress);

            httpResponse = Request.CreateResponse(HttpStatusCode.OK, returnValue);

            return (IHttpActionResult)httpResponse;
        }
        catch (Exception)
        {
            throw;
        }
}

웹 API2 컨트롤러 메서드에 의해 호출되는 데이터 액세스 레이어-int를 반환합니다.

public int RegisterUser(DateTime currentDateTime, string userName, string userPassword, string ipAddress)
{
        int returnedValue = 0;

        try
        {
            dbFunc.OpenDB();

            SqlCommand cmd = new SqlCommand("dbo.RegisterUser", dbFunc.objConn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Clear();

            cmd.Parameters.AddWithValue("@a_CurrentDateTime", currentDateTime);
            cmd.Parameters.AddWithValue("@a_UserName", userName);
            cmd.Parameters.AddWithValue("@a_UserPassword", userPassword);
            cmd.Parameters.AddWithValue("@a_IpAddress", ipAddress);

            cmd.Parameters.Add("@a_UserIdOut", SqlDbType.Int);
            cmd.Parameters["@a_UserIdOut"].Direction = ParameterDirection.Output;

            cmd.ExecuteNonQuery();

            returnedValue = (int)cmd.Parameters["@a_UserIdOut"].Value;

            return returnedValue;
        }
        catch (SqlException sqlex)
        {
            throw sqlex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            // Close the database.
            dbFunc.CloseDB();
        }
}    
G.Dimov

계속 IHttpActionResult시도 하려면 다음을 수행 하십시오.

[HttpGet]
[Route("registeruser/{currentDateTime}/{userName}/{userPassword}/{ipAddress}/")]
public IHttpActionResult RegisterUser(DateTime currentDateTime, string userName, string userPassword, string ipAddress)
{
    try
    {
        IHttpActionResult response;
        HttpResponseMessage httpResponse;

        int returnValue = 0;

        returnValue = dataaccesslayer.RegisterUser(currentDateTime, userName, userPassword, ipAddress);

        httpResponse = Request.CreateResponse(HttpStatusCode.OK, returnValue);

        response = ResponseMessage(httpResponse);
        return response;
    }
    catch (Exception)
    {
        throw;
    }
}

제거해도 문제가 없으면 다음과 같이하십시오.

[HttpGet]
[Route("registeruser/{currentDateTime}/{userName}/{userPassword}/{ipAddress}/")]
public HttpResponseMessage RegisterUser(DateTime currentDateTime, string userName, string userPassword, string ipAddress)
{
    try
    {
        HttpResponseMessage httpResponse;

        int returnValue = 0;

        returnValue = dataaccesslayer.RegisterUser(currentDateTime, userName, userPassword, ipAddress);

        httpResponse = Request.CreateResponse(HttpStatusCode.OK, returnValue);

        return httpResponse;
    }
    catch (Exception)
    {
        throw;
    }
}

둘 사이에 대한 더 긴 논의는 여기 에서이 링크를 확인 하십시오.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관