簡體   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