-1

So I am working on this project where I have to do CRUD operations and I am stuck at getting the user by their id.

This is my function where I get all the users, and I didn't use Entity Framework here. This one works perfectly.

    [Route("AllOperators")]
    public DataSet GetAllOperators()
    {
            DataSet ds = dbLayer.MyValues();
            return ds;
    }

And this is what I have tried to achieve getting a user by his id.

    [HttpGet]
    [Route("api/data/GetById")]
    public IHttpActionResult GetById(string id)
    {

        var result = sl3.Operator.Where(x => x.id == id).FirstOrDefault();

        if (id == null) return NotFound();
        else return result;
    }

Here I used Entity Framework as someone suggested me but I'm getting an error at the end where I return result and it says that

cannot implicitly convert type 'WebApp.Models.Operator' to 'system.web.http.ihttpactionresult'

Can someone please help me fix this bug or even suggest me another way I can get the desired user by his id?

James Z
  • 12,209
  • 10
  • 24
  • 44
Leo Ramadani
  • 223
  • 1
  • 11

6 Answers6

1

If your asp.net version allows it you can switch to the ActionResult response type. It automatically serializes your object to Json.

[HttpGet]
[Route("api/operators/{id}")]
public ActionResult<Operator> GetById([FromRoute] string id)
{

    var result = sl3.Operator.Where(x => x.id == id).FirstOrDefault();

    if (id == null) return NotFound();
    else return Ok(result);
}

Also adjusted your route to be more RESTful. For further information on that, check MS Best Practices API Design Florimont Blog Entry

Bruellhusten
  • 318
  • 1
  • 5
0

sl3.Operator.FirstOrDefault(x => x.id == id); or sl3.Operator.Find(id); is allways true.

You must change:

public IHttpActionResult GetById(string id)

to:

public Operator GetById(string id)

Andre G
  • 11
  • 1
0

If you want to return JSON object try this:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
return Content(JToken.FromObject(result).ToString(), "application/json");

*cannot implicitly convert type 'WebApp.Models.Operator' to 'system.web.http.ihttpactionresult'. This error means your "result" object type is WebApp.Models.Operator. But you need return object which compatible for IHttpActionResult function. Such as Text, JSON, XML ...

data_m
  • 124
  • 1
  • 9
0
public HttpResponseMessage GetById(string id)
{

    var result = sl3.Operator.Where(x => x.id == id).FirstOrDefault();

    if (id == null) throw new HttpResponseException(HttpStatusCode.NotFound);
    else
    {
        return this.Request.CreateResponse<Operator>(
            HttpStatusCode.OK, result);
    }
}
Nicola Biada
  • 2,325
  • 1
  • 8
  • 22
0

The error tells you what your problem is. Your method signature says, you need to return an IHttpActionResult, but result is of a different type.

Also you should change your attributed route, so that id gets properly bound to your parameter.

Change your code to:

[HttpGet]
[Route("api/data/{id}")]
public IHttpActionResult GetById(string id)
{

    var result = sl3.Operator.Where(x => x.id == id).FirstOrDefault();

    if (id == null) 
    {
         return NotFound();
    }
    else 
    {
        return Ok(result);
    }
}

NotFound() is an IHttpActionResult and so is Ok(). Now the compiler should be happy.

You can read up on possible return types here: https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/action-results

Marco
  • 22,856
  • 9
  • 75
  • 124
  • This one actually seems to work. Except that now it fires another error **The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'Operator'. Previously found CLR type 'WebApplication4.Operator', newly found CLR type 'WebApplication4.Models.Operator'.** yikes – Leo Ramadani Aug 12 '21 at 13:23
  • You need to define which class `Operator` you actually want to use as the model. You have two and the compiler can't decide which one ist correct. Either delete one class, if they are redundant or use a more precise declaration in your code. Also see here: https://stackoverflow.com/questions/14927391/the-mapping-of-clr-type-to-edm-type-is-ambiguous-with-ef-6-5 – Marco Aug 12 '21 at 13:54
  • It works perfectly now., thank you for your time, I spent hours on trying to figure it out. Much appreciated. – Leo Ramadani Aug 12 '21 at 14:09
0

You can use it like this to filter record by id

    [HttpGet]
        [Route("api/data/GetById/{id}")]
        public IHttpActionResult GetById(string id)
        {
    
            var result = sl3.Operator.Where(x => x.id.Tolower() == id.ToLower()).FirstOrDefault();    
            if (result == null)
               { 
               return NotFound();
               }
            else return result;
        }
Kamran Khan
  • 1,042
  • 10
  • 21