繁体   English   中英

Web API配置文件C#中的错误

[英]Error in Web API Config file c#

我是C#的新手,我正在开发一个Web API在SQL Server上执行CRUD操作。

我在Web配置文件中收到错误。

我已经创建了模型类:

public class Test_Automation_Data
{
    private String strTest_Automation_Group;
    private String strTest_Automation_Application;
    private String strTest_Automation_Count;

    public string Test_Automation_Group { get => strTest_Automation_Group; set => strTest_Automation_Group = value; }
    public string Test_Automation_Application { get => strTest_Automation_Application; set => strTest_Automation_Application = value; }
    public string Test_Automation_Count { get => strTest_Automation_Count; set => strTest_Automation_Count = value; }
}
public class TestAutomationDataModel
{

    private SqlConnection GetSQLConnection()
    {
        SqlConnection objConnection = null;
        try
        {
            String ConnectionString = "Data Source=**********;Initial Catalog=***********;User ID=************;Password=*********";
            objConnection = new SqlConnection(ConnectionString);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return objConnection;
    }

    public List<Test_Automation_Data> GetTestData()
    {
        List<Test_Automation_Data> Data = new List<Test_Automation_Data>();
        try
        {
            SqlConnection obj_Connection = GetSQLConnection();
            obj_Connection.Open();
            string SQL_Query = "SELECT * FROM Test_Automation_Inventory";
            if (obj_Connection.State == System.Data.ConnectionState.Open)
            {
                SqlCommand objCommand = new SqlCommand(SQL_Query, obj_Connection);
                SqlDataReader DataReader = objCommand.ExecuteReader();
                if (DataReader.HasRows)
                {
                    while (DataReader.Read())
                    {
                        Test_Automation_Data obj_TestData = new Test_Automation_Data();
                        obj_TestData.Test_Automation_Group = DataReader.GetValue(0).ToString();
                        obj_TestData.Test_Automation_Application = DataReader.GetValue(1).ToString();
                        obj_TestData.Test_Automation_Count = DataReader.GetValue(2).ToString();
                        Data.Add(obj_TestData);
                    }
                    DataReader.Close();
                    obj_Connection.Close();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return Data;

    }

    public int DeleteTest(String Test_Group, String Test_Application)
    {
        int RowCount = 0;
        try
        {
            SqlConnection obj_Connection = GetSQLConnection();
            obj_Connection.Open();
            string SQL_Query = "DELETE FROM Test_Automation_Inventory WHERE Test_Inventory_Group = '" + Test_Group + "' and Test_Inventory_Application = '" + Test_Application + "'";
            if (obj_Connection.State == System.Data.ConnectionState.Open)
            {
                SqlCommand objCommand = new SqlCommand(SQL_Query, obj_Connection);
                RowCount = int.Parse(objCommand.ExecuteNonQuery().ToString());
            }
            obj_Connection.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return RowCount;
    }

    public int CreateTest(String Test_Group, String Test_Application)
    {
        int RowCount = 0;
        try
        {
            SqlConnection obj_Connection = GetSQLConnection();
            obj_Connection.Open();
            if (obj_Connection.State == System.Data.ConnectionState.Open)
            {
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandType = System.Data.CommandType.Text;
                objCommand.CommandText = "INSERT INTO Test_Automation_Inventory  Values (@Test_Group,@Test_Application,@Test_Count)";
                objCommand.Connection = obj_Connection;

                objCommand.Parameters.AddWithValue("@Test_Group", Test_Group);
                objCommand.Parameters.AddWithValue("@Test_Application", Test_Application);
                objCommand.Parameters.AddWithValue("@Test_Count", 0);
                RowCount = objCommand.ExecuteNonQuery();
            }
            obj_Connection.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return RowCount;
    }
}

这是我的控制器类:

public class TestController : ApiController
{

    // GET: api/TestAutomationInventory
    public List<Test_Automation_Data> Get()
    {
        TestAutomationDataModel obj = new TestAutomationDataModel();
        return obj.GetTestData();
    }
}

这是我的Web配置文件:

 <compilers>
  <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
  <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>

我在尝试运行项目时收到以下错误:

错误

Line 39:       <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />

如何解决问题

您可能正在使用依赖于此程序集的程序包,假定您已经安装了该程序包。

您可以使用软件包管理器进行安装:

在此处输入图片说明

然后输入以下命令:

PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform

将其安装到全局程序集缓存中可能是一个好主意,这样将来的应用程序就不会遇到相同的问题。

gacutil -i "C:\*PATH TO YOUR APP CODE*\bin\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll"

在Packages文件夹(由Nuget使用),参考文件夹或GAC中的DLL版本不是

Version=1.0.3.0

暂无
暂无

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

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