mercredi 28 novembre 2018

Internal Server Error 500 with Web Api C#

I started developing an API in c# in order to access data on an online SQL server via Azure. When I run the web API in localhost, everythings is ok but when i publish it my api/employees doesn't work like in localhost. The error i get is "500 (Internal Server Error)" Please help me i searched on the internet, haven't found something that could help me.

There is my connectionString :

    <add name="groupea07Entities1" connectionString="metadata=res://*/Models.Employee.csdl|res://*/Models.Employee.ssdl|res://*/Models.Employee.msl;provider=System.Data.SqlClient;provider connection string=&quot;Server=tcp:groupea07.database.windows.net,1433;Initial Catalog=groupea07;Persist Security Info=False;User ID=MyUser;Password=MyPwd;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;&quot;" providerName="System.Data.EntityClient" />   

There is my EmployeesController :

public class EmployeesController : ApiController
{
    private groupea07Entities1 db = new groupea07Entities1();

    // GET: api/Employees  
    public IQueryable<Employee> GetEmployees()
    {
        return db.Employee;
    }



    // PUT: api/Employees/5  

    public HttpResponseMessage PutEmployee(Employee employee)
    {
        if (!ModelState.IsValid)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        }


        db.Entry(employee).State = EntityState.Modified;

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
        }

        return Request.CreateResponse(HttpStatusCode.OK);
    }

    // POST: api/Employees  

    public HttpResponseMessage PostEmployee(Employee employee)
    {
        if (!ModelState.IsValid)
        {
            db.Employee.Add(employee);
            db.SaveChanges();
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, employee);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = employee.EmployeeID }));
            return response;
        }
        else
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        }

    }

    // DELETE: api/Employees/5  

    public HttpResponseMessage DeleteEmployee(Employee employee)
    {
        Employee remove_employee = db.Employee.Find(employee.EmployeeID);
        if (remove_employee == null)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

        db.Employee.Remove(remove_employee);
        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
        }

        return Request.CreateResponse(HttpStatusCode.OK);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool EmployeeExists(int id)
    {
        return db.Employee.Count(e => e.EmployeeID == id) > 0;
    }
}

Thanks in advance ;)




Aucun commentaire:

Enregistrer un commentaire