我有一个外部Webapi,其URL是:https://dhi.eoffice.gov.in/eFileServices/rest/xmldataset/efile/filecreatedsectionwise
我在邮递员中尝试过,并且数据已通过post方法获取,但是我无法在我的mvc应用程序中使用此api。到现在为止,我一直尝试搜索Google,但没有确切答案
我尝试制作一个类,并使用该类在表中使用api,但未得到任何结果
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);
}
}
@model IEnumerable<eofficeWepApi.Models.dhi>
<table class="table table-responsive" style="width:400px">
<tr>
<th>
@Html.DisplayNameFor(model => model.departmentid)
</th>
<th>
@Html.DisplayNameFor(model => model.departmentname)
</th>
<th>
@Html.DisplayNameFor(model => model.efilecreated)
</th>
<th>
@Html.DisplayNameFor(model => model.orgid)
</th>
<th>
@Html.DisplayNameFor(model => model.orgname)
</th>
<th>
@Html.DisplayNameFor(model => model.pfilecreated)
</th>
<th>
@Html.DisplayNameFor(model => model.total)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.departmentid)
</td>
<td>
@Html.DisplayFor(modelItem => item.departmentname)
</td>
<td>
@Html.DisplayFor(modelItem => item.efilecreated)
</td>
<td>
@Html.DisplayFor(modelItem => item.orgid)
</td>
<td>
@Html.DisplayFor(modelItem => item.orgname)
</td>
<td>
@Html.DisplayFor(modelItem => item.pfilecreated)
</td>
<td>
@Html.DisplayFor(modelItem => item.total)
</td>
</tr>
}
</table>
请检查您使用的是哪种类型的API,无论它是Rest服务还是WCF服务。如果它是一项服务,则需要添加服务引用,以便可以使用api方法。您可以在邮递员本身中获得以上api部分的代码:在您的项目中复制并粘贴该代码。