在 C# 中创建 Asp.net Web Api 时遇到问题,详情如下:
Facing issues in Creating Asp.net Web Api in C# with details below:
每当我尝试 运行 我的应用程序时,它都不会执行并显示此错误。
错误:
我试图搜索它,但没有得到任何有用的信息,最重要的是我确实对 Web.config
进行了更改,但仍然无法在我的应用程序中找到 web.config
。任何可以解决此问题的帮助将不胜感激。
我找不到 web.config
文件的解决方案资源管理器图片:
Employee
控制器:
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
型号:
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;
}
}
}
你应该运行你的网站API来自这个地址http://localhost:18084/Employee
每当我尝试 运行 我的应用程序时,它都不会执行并显示此错误。
错误:
我试图搜索它,但没有得到任何有用的信息,最重要的是我确实对 Web.config
进行了更改,但仍然无法在我的应用程序中找到 web.config
。任何可以解决此问题的帮助将不胜感激。
我找不到 web.config
文件的解决方案资源管理器图片:
Employee
控制器:
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
型号:
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;
}
}
}
你应该运行你的网站API来自这个地址http://localhost:18084/Employee