0

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

    }
}
2
  • 1
    Hi, I think your url is wrong. normally you do not point at the source code file. your controller might answer under localhost:<port>/Employee. please share also the controller def and how you expose it in global.asax Commented Jun 15, 2021 at 13:10
  • @CarloCapuano I have updated the question as you mentioned. Commented Jun 15, 2021 at 13:17

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yes this method is working but why cannot I call my controller like in MVC web application whenever I tried to run on it. Like this way I have to always change my URL every time I run my application by default my application is not running.
You should not use name of "Controllers" in URL and the .cs suffix ( MVC web application does not work like this ), this link may help you check it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.