簡體   English   中英

在創建 Asp.net Web Api 時面臨問題,詳細信息如下:

[英]Facing issues in Creating Asp.net Web Api in C# with details below:

每當我嘗試運行我的應用程序時,它都不會執行並顯示此錯誤。

錯誤:

在此處輸入圖像描述

我試圖搜索它,但我沒有得到任何有用的信息,最重要的是我對Web.config進行了更改,但仍然無法在我的應用程序中找到web.config 任何可以解決此問題的幫助將不勝感激。

我找不到web.config文件的解決方案資源管理器的圖像:

在此處輸入圖像描述

Employee Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using My_Work.Models;

namespace My_Work.Controllers
{
    public class EmployeeController : ApiController
    {
        [HttpGet]
        public IEnumerable<Employee> Get(Employee employee)
        {
            Employee emp = new Employee();
            return emp.GetList(employee);
        }

    }
}

Employee Model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

namespace My_Work.Models
{
    public class Employee
    {
        public int EmployeeID { get; set; }
        [Required]
        public string FirstName { get; set; }
        public string LastName { get; set; }
        [Required]
        public string Gender { get; set; }
        [Required]
        [Range(20, 60, ErrorMessage = "Age must be between 20 and  60")]
        [Display(Name = "Age")]
        public int Age { get; set; }

        [Required]
        [Display(Name = "Education Level")]
        public int EducationLevel { get; set; }
        [Range(25000, 500000, ErrorMessage = "Please enter correct value")]
        [Required]
        /* We can control the display of data in a View (UI) using
       display attributes */
        [Display(Name = "Salary")]
        public int Salary { get; set; }
        [Required]
        [EmailAddress]
        public string EmailAddress { get; set; }
        [Required(ErrorMessage = "Please enter hire date")]
        [Display(Name = "Hire Date")]
        [CustomHireDate(ErrorMessage = "Hire Date must be less than or equal to Today's Date")]
        [DataType(DataType.Date)]
        public DateTime? HireDate { get; set; }
        public string City { get; set; }
        public string ImageURL { get; set; }
        [Required]
        [Display(Name = "Upload Photo")]

        string ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        SqlConnection sqlConnection = null;
        SqlCommand cmd = null;
        public List<Employee> GetList(Employee employee)
        {
            List<Employee> employeesList = new List<Employee>();
            sqlConnection = new SqlConnection(ConnectionString);
            string query = string.Empty;
            query = @"select * from Employee";
            cmd = new SqlCommand(query, sqlConnection);
            sqlConnection.Open();
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                employeesList.Add(new Employee
                {

                    EmployeeID = Convert.ToInt32(dataReader["EmployeeId"].ToString()),
                    FirstName = dataReader["FirstName"].ToString(),
                    LastName = dataReader["LastName"].ToString(),
                    Gender = dataReader["Gender"].ToString(),
                    City = dataReader["City"].ToString(),
                    EmailAddress = dataReader["EmailAddress"].ToString(),
                    Age = Convert.ToInt32(dataReader["Age"].ToString()),
                    Salary = Convert.ToInt32(dataReader["Salary"].ToString()),
                    EducationLevel = Convert.ToInt32(dataReader["EducationLevel"].ToString()),
                    HireDate = DateTime.Parse(dataReader["HireDate"].ToString()),
                    ImageURL = dataReader["ImageURL"].ToString(),
                });
                ;
            }
            sqlConnection.Close();
            return employeesList;
        }
        

    }
}

你應該從這個地址http://localhost:18084/Employee運行你的 Web API

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM