2017-01-08 9 views
2

まあ、私は実際にWEBAPIを学び、私は2がWebApiで複数のGETアクションを使用するには?

public class EmployeeApiController : ApiController 
{ 
    public List<Student> GetAllStudents() { ... } 

    public List<Student> EmailChange(string studentName, string Email) { ... } 

    public List<Student> AddressChange(string studentName, string Address) { ... } 
} 

public class Student 
{ 
    public string StudentName { get; set; } 
    public string Address { get; set; } 
    public string Email { get; set; } 
    public static List<Student> students { get; set; } 
} 

この

CONTROLLERのようなメソッドを取得していたシナリオを持っている場合、私はそれぞれのメソッドを呼び出すことはできませんよと仮定しようとしています、私はそれをどうやって行うことができます、私はたくさんのブログがあることを知っていますが、実際にメソッドにアクセスする方法を理解する助けにはなりませんでした。私はこの

WebApiConfigコードのように私の全体のコード

public static void Register(HttpConfiguration config) 
{ 
    config.MapHttpAttributeRoutes(); 

    config.Routes.MapHttpRoute(
     name: "DefaultApi", 
     routeTemplate: "api/{controller}/{action}/{id}", 
     defaults: new { id = RouteParameter.Optional } 
    ); 
} 

コントローラコード

public List<Student> GetAllStudents() 
    { 
     Student.students = new List<Student> { 
      new Student { StudentName="foo",Address="usa",Email="[email protected]"}, 
      new Student { StudentName="joe",Address="mumbai",Email="[email protected]"}, 
      new Student { StudentName="albert",Address="georgia",Email="[email protected]"}, 
      new Student { StudentName="prince",Address="missisipi",Email="[email protected]"} 
     }; 
     return Student.students; 
    } 
    [HttpGet] 
    public List<Student> UpdateEmail(string studentName, string Email) 
    { 
     return Student.students.Select(i => 
     { 
      if (i.StudentName == studentName) 
      { 
       i.Email = Email; 
      } 
      return i; 
     }).ToList(); 

    } 
    [HttpGet] 
    public List<Student> UpdateAddress(string studentName, string Address) 
    { 
     return Student.students.Select(x => 
     { 
      if (x.StudentName == studentName) 
      { 
       x.Address = Address; 
      } 
      return x; 
     }).ToList(); 
    } 
} 

public class Student 
{ 
    public string StudentName { get; set; } 
    public string Address { get; set; } 
    public string Email { get; set; } 
    public static List<Student> students { get; set; } 
} 

作られ、いくつかのブログを経て、私はUpdateEmaiリットルの両方にアクセスする方法に少し混乱していますメソッドとGET要求を使用するメソッドの両方を使用します。

UPDATE 1

私はこの

http://localhost:53711/api/EmployeeApi/UpdateAddressまたは

http://localhost:53711/api/EmployeeApi/UpdateEmail

のような呼び出しを行うとき、私はこの

enter image description here

のようなエラーが出ます210

と私はこのような呼び出しを行うとき、私は

http://localhost:53711/api/EmployeeApi/UpdateEmail/foo/foo enter image description here

+1

通話に使用するルートを追加します。 –

+0

@LijinJohn正しいですか?私はエラーメッセージを誤読しました。問題は、あなたが示した行動のどれもが入力されたURLと一致しないことです。 – Nkosi

+0

@Nkosiあなたは解決策を知っていますか、実際に私はこの問題について頭をはがしています。長い間、あなたは –

答えて

2

いずれかのテンプレートをに変更してください、およびすなわち、あるとして、それ

public static void Register(HttpConfiguration config) { 
    config.MapHttpAttributeRoutes(); 

    config.Routes.MapHttpRoute(
     name: "DefaultApi", 
     routeTemplate: "api/{controller}/{action}/{studentName}" 
    ); 
} 

OR

休暇のテンプレートのような方法を残して:"api/{controller}/{action}/{id}"と変更方法パラメータ(string id,.......)

[HttpGet] 
public List<Student> UpdateEmail(string id, string Email) { ... } 

OR

にあなたを大会に基づくルーチンgと属性ルーティングを使用する

[RoutePrefix("api/EmployeeApi")] 
public class EmployeeApiController : ApiController 
{ 
    //GET api/EmployeeApi 
    [HttpGet] 
    [Route("")] 
    public List<Student> GetAllStudents() { ... } 

    //GET api/EmployeeApi/EmailChange/foo/[email protected] 
    [HttpGet] 
    [Route("EmailChange/{studentName}/{email}")] 
    public List<Student> EmailChange(string studentName, string email) { ... } 

    //GET api/EmployeeApi/AddressChange/foo/China 
    [HttpGet] 
    [Route("AddressChange/{studentName}/{address}")] 
    public List<Student> AddressChange(string studentName, string Address) { ... } 
} 
+0

あなたのすべての労働者が時間を取ってくれてありがとう、webApiの仕事を教えてくれてありがとうございました –

+0

ここにはたくさんの良い文書があります:https://www.asp.net/ web-api/overview – Nkosi

1

あなたWebApConig.csのようなエラーは、次のようになります取得 -

- などの

api/{controller}/{action}/{id} 

は、問題のコール

http://localhost:port/api/Ctrl/action 
+0

ご回答いただきありがとうございます。 –

関連する問題