繁体   English   中英

Web Api使用字符串参数路由到行动

[英]Web Api routing to action with string paramater

看起来像在ASP.NET MVC中一样简单,但是我不知道如何将带有字符串参数的URI映射到控制器上的操作。 所以我在控制器中有以下三个动作:

 //GET: api/Societe
    public IEnumerable<Societe> GetSociete()
    {
        List<Societe> listeSociete = Librairie.Societes.getAllSociete();
        return listeSociete.ToList();                        
    }


    //GET: api/Societe/id
    [ResponseType(typeof(Societe))]
    public IHttpActionResult GetSociete(int id)
    {
        Societe societeRecherchee = Librairie.Societes.getSociete(id);
        if (societeRecherchee == null)
        {
            return NotFound();
        }
        else
        {
            return Ok(societeRecherchee);
        }
    }


    public IHttpActionResult GetSocieteByLibelle(string name)
    {
        Societe societeRecherchee = new Societe();
        if (!string.IsNullOrWhiteSpace(name))
        {
            societeRecherchee = Librairie.Societes.getSocieteByLibelle(name);
            if (societeRecherchee == null)
            {
                return NotFound();
            }
        }
        return Ok(societeRecherchee);
    }

所以我想用动作来映射一个URI:

GetSocieteByLibelle(字符串名称)

我的路由配置是Wep API项目的默认配置。 有人可以解释一下如何将URI映射到该操作吗? 提前致谢 !

看起来两条路由都导致调用相同的方法调用。 尝试将[Route]属性放在其中之一上,如下所示:

[Route("api/Societe/libelle/{name}")]
public IHttpActionResult GetSocieteByLibelle(string name)
{

}

请注意,默认的/ api / Societe / {id}仍应执行第一个操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM