[英]Visual Studio 2015 / asp.net framework : requiredFieldValidator issue
[英]webapi issue in asp.net using visual studio 2015
我是WebAPI
。 我的问题是,如何从另一个ASP.Net Web API使用WebAPI
。 我已经完成了一些代码,但我不知道如何执行带参数和不带参数的GET和POST请求。
这是我的apicontroller
代码:
namespace CelusionWebapi.Controllers
{
public class TestController : ApiController
{
ClientCall client = new ClientCall();
// GET: api/Test
public IEnumerable<string> Get()
{
return new string[] { "Hello ", "Arjun" };
}
// GET: api/Test/5
public string Get(int id)
{
return "Hello This Is Arjun ";
}
// POST: api/Test
public void Post([FromBody]string value)
{
}
public async Task<Employee> CheckMapping(Employee mapping)
{
string Baseurl = ConfigurationManager.AppSettings["TestBaseurl"].ToString();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage Res = await client.GetAsync(string.Format("Get?id={0}", mapping.EmployeeId));
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var EmpResponse = Res.Content.ReadAsStringAsync();
var promapping = JsonConvert.DeserializeObject<IEnumerable<Employee>>(EmpResponse.Result);
mapping = await Res.Content.ReadAsAsync<Employee>();
}
//returning the employee list to view
return mapping;
}
}
}
}
这是我的课:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CelusionWebapi.Models
{
public class Employee
{
public int EmployeeId
{
get;
set;
}
public string EmployeeName
{
get;
set;
}
public string Address
{
get; set;
}
public string Department
{
get;
set;
}
}
}
我的JSON
如下
[{"employeeId":1,"employeeName":"Arjun walmiki","address":"Thane","department":"IT"},{"employeeId":2,"employeeName":"Rahul","address":"Thane","department":"HR"},{"employeeId":3,"employeeName":"Ajay","address":"Thane","department":"Sales"}]
namespace CelusionWebapi.Controllers
{
public class TestController : ApiController
{
private const string Baseurl = ConfigurationManager.AppSettings["TestBaseurl"].ToString();
private static readonly HttpClient client;
public static TestController(){
client = new HttpClient();
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
// GET: api/Test
public Enumerable<string> Get()
{
return new string[] { "Hello ", "Arjun" };
}
// GET: api/Test/5
public string Get(int id)
{
return "Hello This Is Arjun ";
}
// POST: api/Test
public async Task<IActionResult> Post([FromBody]Employee employee)
{
empolyee = await CheckMapping(employee);
... do something with Employee
return this.created(//location, employee);
}
private async Task<Employee> CheckMapping(Employee mapping)
{
HttpResponseMessage Res = await this.client.GetAsync(string.Format("Get?id={0}", mapping.EmployeeId));
if (Res.IsSuccessStatusCode)
{
mapping = await Res.Content.ReadAsAsync<Employee>();
}
//returning the employee list to view
return mapping;
}
}
}
我对代码进行了一些更改,解决方案如下:
namespace CelusionWebapi.Controllers
{
public class TestController : ApiController
{
// GET: api/Test
public async Task<IEnumerable<Employee>> Get()
{
List<Employee> employee = new List<Employee>();
employee = await GetAllEmployees();
return employee;
}
// POST: api/Test
public void Post([FromBody]Models.Employee employee)
{
}
[NonAction]
public async Task<List<Employee>> GetAllEmployees()
{
string Baseurl = ConfigurationManager.AppSettings["TestBaseurl"].ToString();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage Res = await client.GetAsync(string.Format("GetAllEmployees"));
List<Employee> emoloyeelist = new List<Employee>();
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var EmpResponse = Res.Content.ReadAsStringAsync();
emoloyeelist = JsonConvert.DeserializeObject<List<Employee>>(EmpResponse.Result);
}
//returning the employee list to view
return emoloyeelist;
}
}
}
}
谢谢你们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.