SORU
29 ŞUBAT 2012, ÇARŞAMBA


Birden fazla tek denetleyicisi ASP.NET Web API yöntemleri OLSUN

XML Web API benzer yapısı olan bir Dersim vardı:

public class SomeController : ApiController
{
    [WebGet(UriTemplate = "{itemSource}/Items")]
    public SomeValue GetItems(CustomParam parameter) { ... }

    [WebGet(UriTemplate = "{itemSource}/Items/{parent}")]
    public SomeValue GetChildItems(CustomParam parameter, SomeObject parent) { ... }
}

Bireysel yöntemler haritası olabilir bu yana, çok basit, doğru yerde, doğru istek. GET sadece tek bir yöntemi vardı ama aynı zamanda Object bir parametre olan benzer sınıf için, ben başarılı IActionValueBinder kullanılır. Ancak, yukarıda açıklanan durumda, aşağıdaki hata iletisini alabilirsiniz:

Multiple actions were found that match the request: 

SomeValue GetItems(CustomParam parameter) on type SomeType

SomeValue GetChildItems(CustomParam parameter, SomeObject parent) on type SomeType

Hayır şans şimdiye kadar ApiController ama ExecuteAsync yöntemini geçersiz kılarak bu sorunu yaklaşım için çalışıyorum. Bu konuda herhangi bir tavsiye?

Edit: şimdi ASP.NET bu kod devam etmeye çalışıyorum bahsetmek yönlendirme için farklı bir yaklaşım vardır. Web API unuttum Soru, nasıl kodu ASP.NET Web API iş yapabilirim?

CEVAP
29 AĞUSTOS 2012, ÇARŞAMBA


Bu ekstra yöntemlerden destek ALIN ve normal DİNLENME yöntemleri de destek bulduk en iyi yoldur. Senin WebApiConfig için: aşağıdaki yolları ekleyin

routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d " });
routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}");
routes.MapHttpRoute("DefaultApiGet", "Api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", new {action = "Post"}, new {httpMethod = new HttpMethodConstraint(HttpMethod.Post)});

Test sınıfı ile bu çözüm aşağıda doğrulanmadı. Başarıyla benim denetleyicisi her bir yöntem aşağıda vurmayı başardı:

public class TestController : ApiController
{
    public string Get()
    {
        return string.Empty;
    }

    public string Get(int id)
    {
        return string.Empty;
    }

    public string GetAll()
    {
        return string.Empty;
    }

    public void Post([FromBody]string value)
    {
    }

    public void Put(int id, [FromBody]string value)
    {
    }

    public void Delete(int id)
    {
    }
}

Aşağıdaki istekleri desteklediği doğrulandı:

GET /Test
GET /Test/1
GET /Test/GetAll
POST /Test
PUT /Test/1
DELETE /Test/1

NotEylemler 'Al' yöntemi HttpGet bir öznitelik eklemek isteyebilirsiniz. başlamak size ekstra içeri girerseniz

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • campos9896

    campos9896

    24 Mart 2012
  • The White House

    The White Ho

    21 Ocak 2006
  • Viktorija A.

    Viktorija A.

    28 Mart 2009