Asp.net mvc 使用C#mvc中的外部api,而不使用任何模型,只使用xml格式
我有一个外部webapi,其url如下: 我在postman中尝试过,数据是通过post方法获取的,但我无法在我的mvc应用程序中使用此api。到目前为止,我一直在尝试搜索谷歌,但没有得到确切的答案 我尝试创建一个类并使用该类使用表中的api,但没有得到任何结果Asp.net mvc 使用C#mvc中的外部api,而不使用任何模型,只使用xml格式,asp.net-mvc,rest,api,asp.net-web-api,Asp.net Mvc,Rest,Api,Asp.net Web Api,我有一个外部webapi,其url如下: 我在postman中尝试过,数据是通过post方法获取的,但我无法在我的mvc应用程序中使用此api。到目前为止,我一直在尝试搜索谷歌,但没有得到确切的答案 我尝试创建一个类并使用该类使用表中的api,但没有得到任何结果 public class HomeController : Controller { string baseurl = "https://dhi.eoffice.gov.in/eFileServices/";
public class HomeController : Controller
{
string baseurl = "https://dhi.eoffice.gov.in/eFileServices/";
public async Task<ActionResult> Index()
{
List<dhi> dhinfo = new List<dhi>();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(baseurl);
//client.Headers["Content-type"] = "application/xml";
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage Res = await client.GetAsync("rest/xmldataset/efile/filecreatedsectionwise");
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var EmpResponse = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
dhinfo = JsonConvert.DeserializeObject<List<dhi>>(EmpResponse);
}
//returning the employee list to view
return View(dhinfo);
}
}
公共类HomeController:控制器
{
字符串baseurl=”https://dhi.eoffice.gov.in/eFileServices/";
公共异步任务索引()
{
List dhinfo=新列表();
使用(var client=new HttpClient())
{
//传递服务基url
client.BaseAddress=新Uri(baseurl);
//client.Headers[“Content type”]=“application/xml”;
client.DefaultRequestHeaders.Clear();
//定义请求数据格式
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
//使用HttpClient发送查找web api REST服务资源GetAllEmployees的请求
httpresponsemessageres=await client.GetAsync(“rest/xmldataset/efile/filecreatedsectionwise”);
//检查使用HttpClient发送的响应是否成功
如果(Res.IsSuccessStatusCode)
{
//存储从web api接收的响应详细信息
var EmpResponse=Res.Content.ReadAsStringAsync().Result;
//反序列化从web api接收的响应并存储到员工列表中
dhinfo=JsonConvert.DeserializeObject(EmpResponse);
}
//返回要查看的员工列表
返回视图(dhinfo);
}
}
@模型IEnumerable
@DisplayNameFor(model=>model.departmentid)
@Html.DisplayNameFor(model=>model.departmentname)
@DisplayNameFor(model=>model.efilecreated)
@DisplayNameFor(model=>model.orgid)
@DisplayNameFor(model=>model.orgname)
@DisplayNameFor(model=>model.pfilecreated)
@DisplayNameFor(model=>model.total)
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.departmentid)
@DisplayFor(modelItem=>item.departmentname)
@DisplayFor(modelItem=>item.efilecreated)
@DisplayFor(modeleItem=>item.orgid)
@DisplayFor(modelItem=>item.orgname)
@DisplayFor(modelItem=>item.pfilecreated)
@DisplayFor(modelItem=>item.total)
}
请检查您使用的API类型,是Rest服务还是WCF服务。
如果它是一个服务,您需要添加服务引用,从中可以使用api方法。
您可以在postman中获得上述api部分的代码:在项目中复制并通过该代码。试试这个
using (var client = new HttpClient())
{
request = (HttpWebRequest)WebRequest.Create(url);
// request.KeepAlive = false;
//here check for the authentication headers if any
request.PreAuthenticate = true;
//For JSON Response .....
request.Method = "GET";
request.ContentType = "application/json";
response = (HttpWebResponse)request.GetResponse();
string s = response.ToString();
StreamReader reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd().Trim();
}
我在代码中做错了什么,请告诉我,我使用的api是rest服务我应该在视图中返回什么,如果我不返回任何内容,它不会显示任何内容将api结果存储到模型属性中,然后传递到视图中,这样您就可以在视图中使用您的类模型属性。当我使用您的代码时,它会显示未声明纯文本文档的字符编码时出错。如果文档包含US-ASCII范围之外的字符,则在某些浏览器配置中,文档将以乱码文本呈现。文件的字符编码需要在传输协议中声明,或者文件需要使用字节顺序k作为编码签名。localhost:58389您的api是否经过身份验证?并返回哪种格式的响应?我尝试从postman调用api并以xml格式显示输出,我在postman中也使用了相同的url,我尝试使用的api使用的是rest
using (var client = new HttpClient())
{
request = (HttpWebRequest)WebRequest.Create(url);
// request.KeepAlive = false;
//here check for the authentication headers if any
request.PreAuthenticate = true;
//For JSON Response .....
request.Method = "GET";
request.ContentType = "application/json";
response = (HttpWebResponse)request.GetResponse();
string s = response.ToString();
StreamReader reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd().Trim();
}