2016-01-18 9 views
13

コントローラごとに複数のGet()メソッドをサポートしようとしているだけでなく、Web APIからアクセスできる特殊なメソッドもサポートしようとしています。私はMVC 5でこれを行っていますが、MVC 6でどのように行われているのか分からないようです。ありがとう。MVC 6複数の取得メソッド

答えて

15

あなたは、この属性のルーティングリンクを使用することができます -

[Route("api/[controller]")] /* this is the defualt prefix for all routes, see line 20 for overridding it */ 
public class ValuesController : Controller 
{ 
    [HttpGet] // this api/Values 
    public string Get() 
    { 
     return string.Format("Get: simple get"); 
    } 

    [Route("GetByAdminId")] /* this route becomes api/[controller]/GetByAdminId */ 
    public string GetByAdminId([FromQuery] int adminId) 
    { 
     return $"GetByAdminId: You passed in {adminId}"; 
    } 

    [Route("/someotherapi/[controller]/GetByMemberId")] /* note the/at the start, you need this to override the route at the controller level */ 
    public string GetByMemberId([FromQuery] int memberId) 
    { 
     return $"GetByMemberId: You passed in {memberId}"; 
    } 

    [HttpGet] 
    [Route("IsFirstNumberBigger")] /* this route becomes api/[controller]/IsFirstNumberBigger */ 
    public string IsFirstNumberBigger([FromQuery] int firstNum, int secondNum) 
    { 
     if (firstNum > secondNum) 
     { 
      return $"{firstNum} is bigger than {secondNum}"; 
     } 
     return $"{firstNum} is NOT bigger than {secondNum}"; 
    } 
} 

の詳細についてはこちらを参照してください - http://nodogmablog.bryanhogan.net/2016/01/asp-net-5-web-api-controller-with-multiple-get-methods/

+0

ありがとう!通常のネストされたルートを使用できるので、これはより良い解決策でした。 –

+0

親切に私の[質問]を見てください(https://stackoverflow.com/questions/46680893/passing-multiple-parameters-to-web-api-get-method) – faisal1208

25

複数のGetメソッドを同じURLパターンで使用することはできません。属性のルーティングを使用して、異なるURLパターンに複数のGETメソッドを設定することができます。

[Route("api/[controller]")] 
public class IssuesController : Controller 
{ 
    // GET: api/Issues 
    [HttpGet] 
    public IEnumerable<string> Get() 
    { 
     return new string[] { "item 1", "item 2" }; 
    } 

    // GET api/Issues/5 
    [HttpGet("{id}")] 
    public string Get(int id) 
    { 
     return "request for "+ id; 
    } 

    // GET api/Issues/special/5 
    [HttpGet("special/{id}")] 
    public string GetSpecial(int id) 
    { 
     return "special request for "+id; 
    } 
    // GET another/5 
    [HttpGet("~/another/{id}")] 
    public string AnotherOne(int id) 
    { 
     return "request for AnotherOne method with id:" + id; 
    } 
    // GET api/special2/5 
    [HttpGet()] 
    [Route("~/api/special2/{id}")] 
    public string GetSpecial2(int id) 
    { 
     return "request for GetSpecial2 method with id:" + id; 
    } 
} 

あなたは、私がルートパターンを定義するためのHttpGetRoute属性の両方を使用していることがわかります。以上のような構成により

、あなたは

要求URL以下の応答を取得します:yourSite/API /問題/

結果["value1","value2"]

リクエストURL:yourSite/api/issues/4

結果request for 4

リクエストURL:yourSite/API/SPECIAL2/6

結果request for GetSpecial2 method with id:6

リクエストURL:yourSite /別/ 3

結果request for AnotherOne method with id:3

+1

すごいです!どうもありがとうございます。 –

+0

親切に私の[質問](https://stackoverflow.com/questions/46680893/passing-multiple-parameters-to-web-api-get-method)を参照してください – faisal1208

関連する問題