【问题标题】:Attribute Routing with Parameters in Asp.Net Web API 2Asp.Net Web API 2 中带参数的属性路由
【发布时间】:2018-04-05 19:53:38
【问题描述】:

在 Asp.Net Web API 2 属性路由中,如果我调用任何属性,我需要获取具有该属性的所有数据映射列表,但我得到的是满足条件的序列的第一个元素。
我的控制器如下所示:

[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());

    }
}

【问题讨论】:

    标签: c# asp.net-web-api2 asp.net-web-api-routing attributerouting asp.net-web-api-filters


    【解决方案1】:

    其他操作也需要路由模板,在某些情况下还需要参数约束

    更新路线。

    //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);
    }
    

    参考Attribute Routing in ASP.NET Web API 2

    【讨论】:

    • 当我尝试调用字符串名称 Ifcwall 时,我得到了数据列表中的第一组值。但我需要获取与名称 Ifcwall 匹配的数据列表。(也许我们有将返回函数 FirstOrDefault 更改为其他逻辑)如果您知道逻辑,请有人帮我....
    • @MohanSai FirstOrDefault 如果存在则返回第一个匹配项,否则返回 null。您可以使用Where 过滤列表并仅返回匹配项。
    • 现在我需要一个特定范围之间的数据列表,即 (25,75) Breiterange [HttpGet] [Route("{Breite:int:range(25,75)}")]。如果路由正确,则进一步使用哪个逻辑来获取范围为 25 到 75 的 breite 的详细信息列表...
    猜你喜欢
    • 2015-10-09
    • 2014-04-18
    • 1970-01-01
    • 2013-07-10
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    相关资源
    最近更新 更多