#region HttpGet
[HttpGet]
public IEnumerable<Users> Get()
{
return _userList;
}
[HttpGet]
[AllowAnonymous]
public Users GetUserByID(int id)
{
string idParam = HttpContext.Current.Request.QueryString["id"];
var user = _userList.FirstOrDefault(users => users.UserID == id);
if (user == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return user;
}
[HttpGet]
public IEnumerable<Users> GetUserByName(string userName)
{
string userNameParam = HttpContext.Current.Request.QueryString["userName"];
return _userList.Where(p => string.Equals(p.UserName, userName, StringComparison.OrdinalIgnoreCase));
}
[HttpGet]
public IEnumerable<Users> GetUserByNameId(string userName, int id)
{
string idParam = HttpContext.Current.Request.QueryString["id"];
string userNameParam = HttpContext.Current.Request.QueryString["userName"];
return _userList.Where(p => string.Equals(p.UserName, userName, StringComparison.OrdinalIgnoreCase));
}
[HttpGet]
public IEnumerable<Users> GetUserByModel(Users user)
{
string idParam = HttpContext.Current.Request.QueryString["id"];
string userNameParam = HttpContext.Current.Request.QueryString["userName"];
string emai = HttpContext.Current.Request.QueryString["email"];
return _userList;
}
[HttpGet]
public IEnumerable<Users> GetUserByModelUri([FromUri]Users user)
{
string idParam = HttpContext.Current.Request.QueryString["id"];
string userNameParam = HttpContext.Current.Request.QueryString["userName"];
string emai = HttpContext.Current.Request.QueryString["email"];
return _userList;
}
[HttpGet]
public IEnumerable<Users> GetUserByModelSerialize(string userString)
{
Users user = JsonConvert.DeserializeObject<Users>(userString);
return _userList;
}
public IEnumerable<Users> GetUserByModelSerializeWithoutGet(string userString)
{
Users user = JsonConvert.DeserializeObject<Users>(userString);
return _userList;
}
public IEnumerable<Users> NoGetUserByModelSerializeWithoutGet(string userString)
{
Users user = JsonConvert.DeserializeObject<Users>(userString);
return _userList;
}
#endregion HttpGet
#region HttpPost
[HttpPost]
public Users RegisterNone()
{
return _userList.FirstOrDefault();
}
[HttpPost]
public Users RegisterNoKey([FromBody]int id)
{
string idParam = HttpContext.Current.Request.Form["id"];
var user = _userList.FirstOrDefault(users => users.UserID == id);
if (user == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return user;
}
[HttpPost]
public Users Register([FromBody]int id)
{
string idParam = HttpContext.Current.Request.Form["id"];
var user = _userList.FirstOrDefault(users => users.UserID == id);
if (user == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return user;
}
[HttpPost]
public Users RegisterUser(Users user)
{
string idParam = HttpContext.Current.Request.Form["UserID"];
string nameParam = HttpContext.Current.Request.Form["UserName"];
string emailParam = HttpContext.Current.Request.Form["UserEmail"];
var stringContent = base.ControllerContext.Request.Content.ReadAsStringAsync().Result;
return user;
}
[HttpPost]
public string RegisterObject(JObject jData)
{
string idParam = HttpContext.Current.Request.Form["User[UserID]"];
string nameParam = HttpContext.Current.Request.Form["User[UserName]"];
string emailParam = HttpContext.Current.Request.Form["User[UserEmail]"];
string infoParam = HttpContext.Current.Request.Form["info"];
dynamic json = jData;
JObject jUser = json.User;
string info = json.Info;
var user = jUser.ToObject<Users>();
return string.Format("{0}_{1}_{2}_{3}", user.UserID, user.UserName, user.UserEmail, info);
}
[HttpPost]
public string RegisterObjectDynamic(dynamic dynamicData)
{
string idParam = HttpContext.Current.Request.Form["User[UserID]"];
string nameParam = HttpContext.Current.Request.Form["User[UserName]"];
string emailParam = HttpContext.Current.Request.Form["User[UserEmail]"];
string infoParam = HttpContext.Current.Request.Form["info"];
dynamic json = dynamicData;
JObject jUser = json.User;
string info = json.Info;
var user = jUser.ToObject<Users>();
return string.Format("{0}_{1}_{2}_{3}", user.UserID, user.UserName, user.UserEmail, info);
}
#endregion HttpPost
#region HttpPut
[HttpPut]
public Users RegisterNonePut()
{
return _userList.FirstOrDefault();
}
[HttpPut]
public Users RegisterNoKeyPut([FromBody]int id)
{
string idParam = HttpContext.Current.Request.Form["id"];
var user = _userList.FirstOrDefault(users => users.UserID == id);
if (user == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return user;
}
[HttpPut]
public Users RegisterPut([FromBody]int id)
{
string idParam = HttpContext.Current.Request.Form["id"];
var user = _userList.FirstOrDefault(users => users.UserID == id);
if (user == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return user;
}
[HttpPut]
public Users RegisterUserPut(Users user)
{
string idParam = HttpContext.Current.Request.Form["UserID"];
string nameParam = HttpContext.Current.Request.Form["UserName"];
string emailParam = HttpContext.Current.Request.Form["UserEmail"];
var stringContent = base.ControllerContext.Request.Content.ReadAsStringAsync().Result;
return user;
}
[HttpPut]
public string RegisterObjectPut(JObject jData)
{
string idParam = HttpContext.Current.Request.Form["User[UserID]"];
string nameParam = HttpContext.Current.Request.Form["User[UserName]"];
string emailParam = HttpContext.Current.Request.Form["User[UserEmail]"];
string infoParam = HttpContext.Current.Request.Form["info"];
dynamic json = jData;
JObject jUser = json.User;
string info = json.Info;
var user = jUser.ToObject<Users>();
return string.Format("{0}_{1}_{2}_{3}", user.UserID, user.UserName, user.UserEmail, info);
}
[HttpPut]
public string RegisterObjectDynamicPut(dynamic dynamicData)
{
string idParam = HttpContext.Current.Request.Form["User[UserID]"];
string nameParam = HttpContext.Current.Request.Form["User[UserName]"];
string emailParam = HttpContext.Current.Request.Form["User[UserEmail]"];
string infoParam = HttpContext.Current.Request.Form["info"];
dynamic json = dynamicData;
JObject jUser = json.User;
string info = json.Info;
var user = jUser.ToObject<Users>();
return string.Format("{0}_{1}_{2}_{3}", user.UserID, user.UserName, user.UserEmail, info);
}
#endregion HttpPut
#region HttpDelete
[HttpDelete]
public Users RegisterNoneDelete()
{
return _userList.FirstOrDefault();
}
[HttpDelete]
public Users RegisterNoKeyDelete([FromBody]int id)
{
string idParam = HttpContext.Current.Request.Form["id"];
var user = _userList.FirstOrDefault(users => users.UserID == id);
if (user == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return user;
}
[HttpDelete]
public Users RegisterDelete([FromBody]int id)
{
string idParam = HttpContext.Current.Request.Form["id"];
var user = _userList.FirstOrDefault(users => users.UserID == id);
if (user == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return user;
}
[HttpDelete]
public Users RegisterUserDelete(Users user)
{
string idParam = HttpContext.Current.Request.Form["UserID"];
string nameParam = HttpContext.Current.Request.Form["UserName"];
string emailParam = HttpContext.Current.Request.Form["UserEmail"];
var stringContent = base.ControllerContext.Request.Content.ReadAsStringAsync().Result;
return user;
}
[HttpDelete]
public string RegisterObjectDelete(JObject jData)
{
string idParam = HttpContext.Current.Request.Form["User[UserID]"];
string nameParam = HttpContext.Current.Request.Form["User[UserName]"];
string emailParam = HttpContext.Current.Request.Form["User[UserEmail]"];
string infoParam = HttpContext.Current.Request.Form["info"];
dynamic json = jData;
JObject jUser = json.User;
string info = json.Info;
var user = jUser.ToObject<Users>();
return string.Format("{0}_{1}_{2}_{3}", user.UserID, user.UserName, user.UserEmail, info);
}
[HttpDelete]
public string RegisterObjectDynamicDelete(dynamic dynamicData)
{
string idParam = HttpContext.Current.Request.Form["User[UserID]"];
string nameParam = HttpContext.Current.Request.Form["User[UserName]"];
string emailParam = HttpContext.Current.Request.Form["User[UserEmail]"];
string infoParam = HttpContext.Current.Request.Form["info"];
dynamic json = dynamicData;
JObject jUser = json.User;
string info = json.Info;
var user = jUser.ToObject<Users>();
return string.Format("{0}_{1}_{2}_{3}", user.UserID, user.UserName, user.UserEmail, info);
}
#endregion HttpDelete
<script>
$(function () {
var user = { UserID: "11", UserName: "Eleven", UserEmail: "[email protected]" };
var info = "this is muti model";
$("#btnGet1").on("click", function () {
$.ajax({
url: "/api/users/GetUserByName", type: "get", data: { "username": "Superman" },
beforeSend: function (XHR) {
XHR.setRequestHeader('Authorization', 'BasicAuth ' + ticket);
},
success: function (data) {
alert(data);
}, datatype: "json"
});
});
$("#btnGet2").on("click", function () {
$.ajax({ url: "/api/users/GetUserByID", type: "get", data: { "id": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet3").on("click", function () {
$.ajax({ url: "/api/users/GetUserByNameId", type: "get", data: { "userName": "Superman", "id": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet4").on("click", function () {
$.ajax({ url: "/api/users/Get", type: "get", data: "", success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet5").on("click", function () {
$.ajax({ url: "/api/users/GetUserByModel", type: "get", data: user, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet6").on("click", function () {
$.ajax({ url: "/api/users/GetUserByModelUri", type: "get", data: user, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet7").on("click", function () {
$.ajax({ url: "/api/users/GetUserByModelSerialize", type: "get", data: { userString: JSON.stringify(user) }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet8").on("click", function () {
$.ajax({ url: "/api/users/GetUserByModelSerializeWithoutGet", type: "get", data: { userString: JSON.stringify(user) }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnGet9").on("click", function () {
$.ajax({ url: "/api/users/NoGetUserByModelSerializeWithoutGet", type: "get", data: { userString: JSON.stringify(user) }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPost1").on("click", function () {
$.ajax({ url: "/api/users/RegisterNoKey", type: "post", data: { "": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPost2").on("click", function () {
$.ajax({ url: "/api/users/Register", type: "post", data: { "id": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPost3").on("click", function () {
$.ajax({ url: "/api/users/RegisterUser", type: "post", data: user, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPost4").on("click", function () {
$.ajax({ url: "/api/users/RegisterUser", type: "post", data: JSON.stringify(user), success: function (data) { alert(data); }, datatype: "json", contentType: 'application/json', });
});
$("#btnPost5").on("click", function () {
$.ajax({ url: "/api/users/RegisterObject", type: "post", data: { "User": user, "Info": info }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPost6").on("click", function () {
$.ajax({ url: "/api/users/RegisterObjectDynamic", type: "post", data: { "User": user, "Info": info }, success: function (data) { alert(data); }, datatype: "json", contentType: 'application/json' });
});
$("#btnPut1").on("click", function () {
$.ajax({ url: "/api/users/RegisterNoKeyPut", type: "put", data: { "": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPut2").on("click", function () {
$.ajax({ url: "/api/users/RegisterPut", type: "put", data: { "id": $("#txtId").val() }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPut3").on("click", function () {
$.ajax({ url: "/api/users/RegisterUserPut", type: "put", data: user, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPut4").on("click", function () {
$.ajax({ url: "/api/users/RegisterUserPut", type: "put", data: JSON.stringify(user), success: function (data) { alert(data); }, datatype: "json", contentType: 'application/json', });
});
$("#btnPut5").on("click", function () {
$.ajax({ url: "/api/users/RegisterObjectPut", type: "put", data: { "User": user, "Info": info }, success: function (data) { alert(data); }, datatype: "json" });
});
$("#btnPut6").on("click", function () {
$.ajax({ url: "/api/users/RegisterObjectDynamicPut", type: "put", data: { "User": user, "Info": info }, success: function (data) { alert(data); }, datatype: "json", contentType: 'application/json' });
});
$("#btnLogin").on("click", function () {
$.ajax({
url: "/api/users/Login", type: "GET", data: { "Account": $("#txtAccount").val(), "Password": $("#txtPassword").val() }, success: function (data) {
var result = JSON.parse(data);
if (result.Result) {
ticket = result.Ticket;
alert(result.Ticket);
}
else {
alert("failed");
}
}, datatype: "json"
});
});
var ticket = "";
jQuery.support.cors = true;
var location = "http://localhost:9008";
$("#btnGetCors1").on("click", function () {
$.ajax({ url: location + "/api/users/GetUserByID", type: "get", data: { "id": 1 }, success: function (data) { alert(data); }, datatype: "json" });
});
});
</script>