仅使用xml格式就可以在C#mvc中使用没有任何模型的外部api

时间:2019-09-30 07:11:53

标签: asp.net-mvc rest api asp.net-web-api

我有一个外部Webapi,其网址是: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>  

2 个答案:

答案 0 :(得分:0)

请检查您使用的API类型,无论是Rest服务还是WCF服务。 如果它是一项服务,则需要添加服务引用,以便可以使用api方法。 您可以在邮递员本身中获得上述api部分的代码:在项目中复制并粘贴该代码。

答案 1 :(得分:0)

尝试一下

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();
                }