呼叫不是从web api中的控制器传递到模型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了呼叫不是从web api中的控制器传递到模型相关的知识,希望对你有一定的参考价值。

我是web api的新手。我创建了web api,只是为了从我在互联网上找到的一些文章的帮助下从oracle DB中检索数据。我试图从早上发现错误,但直到现在都没有成功。当我尝试运行代码时,它不会给出任何错误或任何错误。调试器传递给我的数据库类并停止。下面是我的控制器代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using System.Data.SqlClient;


namespace iLearnWebApi.Controllers
{
public class ValuesController : ApiController
{
    DAL.DBAccess dblayer = new DAL.DBAccess();
    public DataSet Getrecord(int programid)
    {            
        DataSet ds = dblayer.GetRecordbyid(programid);
        return ds;
    }
}   }

以下是我的DBClass代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Configuration;
using System.Data.Odbc;
using Oracle.ManagedDataAccess.Client;


namespace iLearnWebApi.DAL
{
 public class DBAccess
{
    OracleParameter[] _objOraParam;
    OracleConnection con = new OracleConnection();
    //--- Get Records By Program ID
    public DataSet GetRecordbyid(int progid)
    {            
        DataSet ds = new DataSet();
        try
        {
            _objOraParam = new OracleParameter[2];

            _objOraParam[0] = new OracleParameter("p_Program_ID", OracleDbType.Int32);
            _objOraParam[0].Direction = ParameterDirection.Input;
            _objOraParam[0].Value = progid;

            _objOraParam[1] = new OracleParameter("RCT_OUT", OracleDbType.RefCursor);
            _objOraParam[1].Direction = ParameterDirection.Output;

            ds = ExecuteDataset(con, CommandType.StoredProcedure, "ILS_USP_PROGRAM_DATA", _objOraParam);

        }
        catch (Exception ex)
        {
            LogError("GetRecordbyid", ex.Message.ToString());
            throw ex;
        }


        finally
        {
            con.Close();
        }
        return ds;
    }



    // Execute Data
    private DataSet ExecuteDataset(OracleConnection con, CommandType procname, string commandText, params OracleParameter[] objOraParam)
    {
        //create a command and prepare it for execution
        OracleCommand cmd = new OracleCommand();
        PrepareCommand(cmd, con, (OracleTransaction)null, procname, commandText, objOraParam);

        //create the DataAdapter & DataSet
        OracleDataAdapter da = new OracleDataAdapter(cmd);
        DataSet ds = new DataSet();

        //fill the DataSet using default values for DataTable names, etc.
        da.Fill(ds);
        cmd.Dispose();
        //return the dataset
        return ds;
    }


    //---- Used To Prepare Oracle command
    private void PrepareCommand(OracleCommand command, OracleConnection connection, OracleTransaction transaction, CommandType commandType, string commandText, OracleParameter[] commandParameters)
    {
        //if the provided connection is not open, we will open it
        string con_string = ConfigurationManager.ConnectionStrings["OracleConnectionString"].ConnectionString;

        if (connection.State != ConnectionState.Open)
        {
            connection.ConnectionString = DecryptString(con_string, "CFSENC");
            connection.Open();
        }

        //associate the connection with the command
        command.Connection = connection;

        //set the command text (stored procedure name or Oracle statement)
        command.CommandText = commandText;

        //if we were provided a transaction, assign it.
        if (transaction != null)
        {
            command.Transaction = transaction;
        }

        //set the command type
        command.CommandType = commandType;

        //attach the command parameters if they are provided
        if (commandParameters != null)
        {
            AttachParameters(command, commandParameters);
        }

        return;
    }


    // Used For Attaching Parameter To Command
    private void AttachParameters(OracleCommand command, OracleParameter[] commandParameters)
    {
        foreach (OracleParameter p in commandParameters)
        {
            //check for derived output value with no value assigned
            if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
            {
                p.Value = DBNull.Value;
            }

            command.Parameters.Add(p);
        }
    }



    // Used For Decryption Of Encrypted String
    private string DecryptString(string con_string, string key)
    {
        byte[] plainBytes = null;
        try
        {

            string passWord = key;
            string strInput = con_string;

            byte[] encryptBytes = Convert.FromBase64String(strInput);
            MemoryStream ms = new MemoryStream(strInput.Length);
            //Using triple des for decryption
            TripleDESCryptoServiceProvider tDesCsp = new TripleDESCryptoServiceProvider();
            // Creating  decryption IV and Key using the key supplied by the user
            tDesCsp.IV = new byte[8];
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(passWord, new byte[0]);
            tDesCsp.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
            CryptoStream deEnStream = new CryptoStream(ms, tDesCsp.CreateDecryptor(), CryptoStreamMode.Write);
            //write the decrypted data to the stream
            deEnStream.Write(encryptBytes, 0, encryptBytes.Length);
            deEnStream.FlushFinalBlock();
            plainBytes = new byte[ms.Length];
            ms.Position = 0;
            //reading the decrypted stream and write it  into the byte array
            ms.Read(plainBytes, 0, (int)ms.Length);
            deEnStream.Close();
        }
        catch (Exception err)
        {
            string sErr = err.ToString();
            throw new Exception("Error decrypting string.");
        }
        return Encoding.UTF8.GetString(plainBytes);
    }


    // For Writing Log Files
    private void LogError(string header, string error)
    {
        string strPath;
        string strActualError;
        StreamWriter objErrWriter;
        DateTime objDt = DateTime.Now;
        string strDate;
        strDate = objDt.ToString("ddMMyyyy");

        try
        {
            // Get Actual Path of "Error" stored in Web.config
            strPath = ConfigurationManager.AppSettings["sPathErrorLog"];
            //Generates Path & LogFile Name of  ErrorLog
            strPath = strPath + strDate + ".log";
            // Generates Error Message
            strActualError = DateTime.Now + " : " + header + " : " + error;

            // Creation of File.
            objErrWriter = new StreamWriter(strPath, true, System.Text.Encoding.ASCII);

            objErrWriter.WriteLine("");
            objErrWriter.WriteLine(strActualError);
            objErrWriter.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
}}

任何人都可以告诉我在上面的代码中我做了什么错误。

答案

这听起来像路由问题(没有看到您的路由配置)。

尝试改变:public DataSet Getrecord(int programid)

致:public DataSet Get(int id)

并致电:localhost:60908 / api / Values / 1

以上是关于呼叫不是从web api中的控制器传递到模型的主要内容,如果未能解决你的问题,请参考以下文章

从angularjs传递参数至Web API

Spring MVC @Controller中转发或者重定向到其他页面的信息怎么携带和传递(Servlet API对象)HttpServletRequestHttpServletRespose(代码片

Web API 接口版本控制 SDammann.WebApi.Versioning

如何在WEB API中将多个模型作为参数传递

Web API 控制器不尊重 CORS 来源

通过 insert_id() 插入数据库表的最后一个 id,而不是从 Codeigniter 中的模型返回到控制器文件