3

In Asp.Net Web API 2 Attribute routing, If i call any attribute i need to get all list of data mapping with that attribute but I am getting the first element of a sequence that satisfies a condition.
My controller looks like this:

[RoutePrefix("api/Ifcobjects")] public class IfcobjectsController : ApiController { static List<Ifcobject> Ifcobjects = new List<Ifcobject>() { new Ifcobject() { Id = 1,Ifctype="Ifcwall", Name = "Stdwall",Tag="Wall",Material="Beton",Breite=25,Betonklasse="C30/37" }, new Ifcobject() { Id = 2,Ifctype="Ifcwall", Name = "Stdwall",Tag="Wall",Material="Beton",Breite=50 }, new Ifcobject() { Id = 3,Ifctype="Ifcwall", Name = "Stdwall50cm",Tag="Wall",Material="Beton",Breite=75 }, new Ifcobject() { Id = 4,Ifctype="Ifcbeam", Name = "beam",Tag="Beam",Material="Beton",Breite=100 } }; public IHttpActionResult Get() { return Ok(Ifcobjects); } public IHttpActionResult Get(int id) { var Ifcobject = Ifcobjects.FirstOrDefault(s => s.Id == id); if (Ifcobject == null) { //return NotFound(); return Content(HttpStatusCode.NotFound, "Ifcobject not found"); } return Ok(Ifcobject); } [Route("{Ifctype:alpha}")] public Ifcobject Get(string ifctype) { return Ifcobjects.FirstOrDefault(s => s.Ifctype.ToLower() == ifctype.ToLower()); } } 

1 Answer 1

1

Other actions will need route templates as well and in some cases parameter constraints

Update routes.

//GET api/Ifcobjects [HttpGet] [Route("")] public IHttpActionResult Get() { return Ok(Ifcobjects); } //GET api/Ifcobjects/1 [HttpGet] [Route("{id:int}")] public IHttpActionResult Get(int id) { var Ifcobject = Ifcobjects.FirstOrDefault(s => s.Id == id); if (Ifcobject == null) { //return NotFound(); return Content(HttpStatusCode.NotFound, "Ifcobject not found"); } return Ok(Ifcobject); } //GET api/Ifcobjects/Ifcwall [HttpGet] [Route("{Ifctype:alpha}")] public IHttpActionResult Get(string ifctype) { var results = Ifcobjects.Where(s => s.Ifctype.ToLower() == ifctype.ToLower()).ToList(); if(results.Count == 0) { return Content(HttpStatusCode.NotFound, "ifctype not found"); } return Ok(results); } 

Reference Attribute Routing in ASP.NET Web API 2

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

3 Comments

when I am trying to call string name Ifcwall I am getting first set of values in list of data.But I need to get the list of data which matches with the name Ifcwall.(May be we have to change return function FirstOrDefault to other logic) can some one please help me if you know the logic ....
@MohanSai FirstOrDefault returns the first match if it exists else null if it does not.You can use Where to filter list and return only the matches.
Now I need a list of data for the breite in between a specific range i.e (25,75) like that.... I am trying to route like this : //GET api/Ifcobjects/Breiterange [HttpGet] [Route("{Breite:int:range(25,75)}")]. if routing is correct which logic is to be used further for this to get list of details for breite for range 25 to 75...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.