Pregunta

Right now I'm doing a test API I'm using Postman extension to test the POST methods on google chrome. I have a database in my local server, I'm using VS2010 with framework 4.0.

My API right now has method GetByID working perfectly.

They all clear the data before sending the new one at each request, but this doesn't happen when i make a POST request.

Everytime i make the request the previous result stays there ( and if i change the parameters of the request the number of results stay the same but the data changes).

private List<Employee> Employees = new List<Employee>();

SqlConnection con;
SqlDataAdapter da;
DataSet ds = new DataSet();

public IEnumerable<Employee> Post(string param1, string param2)
{
    ds.Clear();
    con = new SqlConnection("Server=xxxx; Database=x; Trusted_Connection=True;");
    da = new SqlDataAdapter("select * from Employee", con);           
    da.Fill(ds);

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        Employees.Add(new Employee() { 
                          FirstName = dr[0].ToString(), 
                          LastName = dr[1].ToString(), 
                          Id = int.Parse(dr[2].ToString()), 
                          Designation = dr[3].ToString() 
                     });

    }

    return Employees.Where(e => e.FirstName == param1)
                    .Where(e => e.LastName == param2);
}

My employee controller

static readonly EmployeeDetails repository = new EmployeeDetails();

[System.Web.Mvc.HttpPost]
public IEnumerable<Employee> Search(string param1, string param2)
{
    //Employee search = repository.Post(param1, param2);
    IEnumerable<Employee> search = repository.Post(param1, param2);
    if (search == null)
    {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }

    return search;
}

I've been playing with this for an entire day now and didn't find any solution to the problem. Can anyone help?

Thank you

¿Fue útil?

Solución 2

Ok guys this is the 2nd queston i post on stackoverflow and again the answer is pretty simple and really stupid.

The variable Employees was a global one, and i wasnt cleaning it i wa cleaning only the dataset everytime it was calling again employees.

i just needed to add Employees.Clear();

Sorry for the time wasted :p

Otros consejos

It may be because of Cache try to add this attribute over post method

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top