如何在 web API 中为对象 class 构建 [HttpPost] 方法? (Xamarin.form + 网络 API)(update1)
How to build a [HttpPost] method in web API for object class? (Xamarin.form + web API)(update1)
我正在尝试 xamarin.form 在网络上注册一个管理员帐户 API 通过在我的 XAML 和 register/post 中输入值来与预定义值结合到网络 API。不幸的是,我仍然是 Xamarin 平台的初学者。
我面临的问题是 在 AdminAccountController.cs 中制作 [HttpPost] 方法的主体 class对象。
(已更新 1)
-成功 return 当 POST 请求
时在 Postman 中响应
POSTMAN GET REQUEST 响应(成功):-
[
{
"id": 1,
"username": "admin1",
"password": "12345678"
},
{
"id": 2,
"username": "admin2",
"password": "12345678"
}
]
POSTMAN POST REQUEST 响应(成功由@jason 提供):-
{
"id": 3,
"username": "admin3",
"password": "12345678"
}
AdminAccountController.cs(更新)
using FoodWebApi.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace FoodWebApi.Controllers
{
public class AdminAccountController : ApiController
{
List<Admin> admins = new List<Admin>()
{
new Admin
{
id=1,
username="admin1",
password="12345678"
},
new Admin
{
id=2,
username="admin2",
password="12345678"
}
};
//http://localhost:53287/api/AdminAccount
public IEnumerable<Admin> GetAll()
{
return admins;
}
//http://localhost:53287/api/AdminAccount/1
public IHttpActionResult GetById(int id)
{
var admin = admins.FirstOrDefault(x => x.id == id);
if (admin == null)
{
return NotFound();
}
return Ok(admin);
}
[HttpPost]
public Admin PostNewAdmin(Admin admin)
{
// add the new admin to your list
admins.Add(admin);
// return to the caller
return admin;
}
}
}
Admin.cs
namespace FoodWebApi.Models
{
public class Admin
{
public int id { set; get; }
public string username { set; get; }
public string password { set; get; }
}
}
首先,使用列表而不是数组,它更简单
// need this for List
using System.Collections.Generic;
...
List<Admin> admins = new List<Admin>()
{
new Admin
{
id=1,
username="admin1",
password="12345678"
},
new Admin
{
id=2,
username="admin2",
password="12345678"
}
};
[HttpPost]
public Admin PostNewAdmin(Admin admin)
{
// add the new admin to your list
admins.Add(admin);
// return to the caller
return admin;
}
我正在尝试 xamarin.form 在网络上注册一个管理员帐户 API 通过在我的 XAML 和 register/post 中输入值来与预定义值结合到网络 API。不幸的是,我仍然是 Xamarin 平台的初学者。
我面临的问题是 在 AdminAccountController.cs 中制作 [HttpPost] 方法的主体 class对象。
(已更新 1)
-成功 return 当 POST 请求
时在 Postman 中响应POSTMAN GET REQUEST 响应(成功):-
[
{
"id": 1,
"username": "admin1",
"password": "12345678"
},
{
"id": 2,
"username": "admin2",
"password": "12345678"
}
]
POSTMAN POST REQUEST 响应(成功由@jason 提供):-
{
"id": 3,
"username": "admin3",
"password": "12345678"
}
AdminAccountController.cs(更新)
using FoodWebApi.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace FoodWebApi.Controllers
{
public class AdminAccountController : ApiController
{
List<Admin> admins = new List<Admin>()
{
new Admin
{
id=1,
username="admin1",
password="12345678"
},
new Admin
{
id=2,
username="admin2",
password="12345678"
}
};
//http://localhost:53287/api/AdminAccount
public IEnumerable<Admin> GetAll()
{
return admins;
}
//http://localhost:53287/api/AdminAccount/1
public IHttpActionResult GetById(int id)
{
var admin = admins.FirstOrDefault(x => x.id == id);
if (admin == null)
{
return NotFound();
}
return Ok(admin);
}
[HttpPost]
public Admin PostNewAdmin(Admin admin)
{
// add the new admin to your list
admins.Add(admin);
// return to the caller
return admin;
}
}
}
Admin.cs
namespace FoodWebApi.Models
{
public class Admin
{
public int id { set; get; }
public string username { set; get; }
public string password { set; get; }
}
}
首先,使用列表而不是数组,它更简单
// need this for List
using System.Collections.Generic;
...
List<Admin> admins = new List<Admin>()
{
new Admin
{
id=1,
username="admin1",
password="12345678"
},
new Admin
{
id=2,
username="admin2",
password="12345678"
}
};
[HttpPost]
public Admin PostNewAdmin(Admin admin)
{
// add the new admin to your list
admins.Add(admin);
// return to the caller
return admin;
}