sanjay sanjay - 1 year ago 73
C# Question

i want to get name from the webapi . but its throwing error showing. cannot implicitly convert type model to ihttpactionresult

i have created one class "student". added 3 elements in the list. i want to get one name . but its giving compile time error.

using testWEBAPI.Models;

namespace testWEBAPI.Controllers
public class MyapiController : ApiController
public List<student> s = new List<student>()
new student{name="krishna",city="hyderbad"},
new student{name="priya",city="mumbai"},
new student{name="sandeep",city="jamshedpur"}

public IHttpActionResult getnames()
return Ok(s.ElementAt(0).name);



name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

desired out put - krishna,hyderabad

Answer Source

You need to do:

return Ok(s.ElementAt(0));

This is happening because the method is expecting you to return IHttpActionResult.

Alternatively you can change the method to expect a Student object to be returned.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download