2

i made a Entity Data Model From Database Table Like this. enter image description here

Then add 4 line of codes in WebApiConfig.cs to get json data from response.

var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        config.Formatters.Remove(config.Formatters.XmlFormatter);

Then i created a Controller (Web API 2 controller with read/Write action) named importController.

then i added this codes to add and retrieve data from database. but not working.

    CodeXenETSEntities db = new CodeXenETSEntities();  

    [HttpPost]
    [ActionName("pushlocation")]
    // POST: api/pushlocation  
    public HttpResponseMessage pushlocation( int userid,decimal longitude, decimal latitude , TimeSpan time )
    {
        user_locations ulog = new user_locations();
        ulog.user_id = userid;
        ulog.lon = longitude;
        ulog.lat = latitude;
        ulog.time = time;
        db.user_locations.Add(ulog);
        db.SaveChanges();
        return Request.CreateResponse(HttpStatusCode.Accepted, "Successfully Created");
    }


    [HttpGet]
    [ActionName("pulllocation")]
    // GET: api/pulllocation/5  
    public HttpResponseMessage pulllocation(int userid, decimal longitude, decimal latitude, TimeSpan time)
    {

        db.user_locations.ToList();
        return Request.CreateResponse(HttpStatusCode.Accepted, "Success");
    }

this is the output : enter image description here

Database Data :

enter image description here

4
  • I believe your url should be something along the lines of localhost:57715/api/import/pulllocation Commented Sep 15, 2016 at 20:40
  • Also, your route config in WebApiConfig.cs may not allow for parameterless calls since you have specified four parameters in your pulllocation method Commented Sep 15, 2016 at 22:02
  • @AndyArndt thanks for the suggestion. but still not working. any other idea or suggestion ? Commented Sep 16, 2016 at 6:03
  • @sly what to do then? please advice me Commented Sep 16, 2016 at 6:04

1 Answer 1

1

3 things:

1) In WebApiConfig.cs use routeTemplate: "api/{controller}/{action}/{id}"

2) In importController change public HttpResponseMessage pulllocation(int userid, decimal longitude, decimal latitude, TimeSpan time) to public HttpResponseMessage pulllocation()

3) In Browser address bar use http://localhost:57715/api/{controller name}/pulllocation. in your case http://localhost:57715/api/import/pulllocation

I hope this will solve

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

1 Comment

Thanks Man. you saved me :D

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.