Mark Jordan
Mark Jordan

Reputation: 37

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

Whenever I tried to run my application it will not execute and show this error.

Error:

enter image description here

I have tried to search it but I did not get any useful information about it and most of all I did make changes to Web.config but still cannot find the web.config in my application. Any help which could solve this problem will be appreciated.

Image of Solution Explorer where I cannot find web.config file:

enter image description here

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;
        }
        

    }
}

Upvotes: 0

Views: 283

Answers (1)

Rostam Bamasi
Rostam Bamasi

Reputation: 274

you should run your Web API from this address http://localhost:18084/Employee

Upvotes: 2

Related Questions