Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I currently have a web api Get method using EF6 and it is accepting an int parameter called serial. Instead of the primary key serial I want to find extserial from the URI.

Here is my Get where I have localhost/api/AH?serial=1

C#
// GET api/AH/5
[ResponseType(typeof(Transmital))]
public IHttpActionResult GetTransmital(int Serial)
{
    Transmital transmital = db.Transmitals.Find(Serial);
    if (transmital == null)
    {
        return NotFound();
    }

    return Ok(transmital);
}


I need to have it get by localhost/api/AH?ExtSerial=ABC123

For some reason it won't take the following

C#
// GET api/AH/5
        [ResponseType(typeof(Transmital))]
        public IHttpActionResult GetTransmital(string ExtSerial) //or [FromUri]string ExtSerial
        {
            Transmital transmital = db.Transmitals.Find(ExtSerial);
            if (transmital == null)
            {
                return NotFound();
            }

            return Ok(transmital);
        }
Posted
Updated 25-Apr-15 2:07am
v2
Comments
Maciej Los 25-Apr-15 7:53am    
Debug the program to find out the reasons above code does not work.
Joshua Masa 25-Apr-15 7:57am    
I tried. When I tried to GET extserial using Postman it just gets the whole table. Not sure what I'm missing.
Maciej Los 25-Apr-15 8:06am    
Use "Reply" widget.
Find method - might be reason of troubles.
Joshua Masa 25-Apr-15 8:20am    
I see. What can I use then? something like db.Transmital.ExtSerial?
Maciej Los 25-Apr-15 8:47am    
https://msdn.microsoft.com/en-us/data/jj573936.aspx

1 solution

Never mind. I got it.

I used an IHttpActionResult interface instead.

C#
public IHttpActionResult GetTransmitalsExtSerial([FromUri] string ExtSerial)
        {
            return Ok(db.Transmitals.Where(exs => exs.ExtSerial == ExtSerial));
        }



Thank you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900