【问题标题】:Consume an external api in C# mvc without any model , just simply in xml format在 C# mvc 中使用外部 api,无需任何模型,只需 xml 格式
【发布时间】:2020-01-29 11:09:32
【问题描述】:

我有一个外部 webapi,其 url 是这样的:https://dhi.eoffice.gov.in/eFileServices/rest/xmldataset/efile/filecreatedsectionwise

我在 postman 中尝试过,并且在 post 方法中获取了数据,但我无法在我的 mvc 应用程序中使用这个 api。直到现在我已经尝试搜索谷歌但没有得到确切的答案

我尝试创建一个类并使用该类来消费表中的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>  

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 我在我的代码中做错了什么,你能告诉我,我正在使用的 api 是休息服务
    【解决方案2】:

    试试这个

    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 结果存储到 Model 属性中,然后传递给 View ,这样您就可以在 View 中使用您的类模型属性。
    • 当我使用您的代码时,它在控制台中显示错误未声明纯文本文档的字符编码。如果文档包含 US-ASCII 范围之外的字符,则文档将在某些浏览器配置中呈现乱码。文件的字符编码需要在传输协议中声明或者文件需要使用字节顺序标记作为编码签名。本地主机:58389 ​
    • 您的 api 是否经过身份验证?并以何种格式返回响应?
    • 我尝试从邮递员调用 api 并以 xml 格式显示输出,我也在邮递员中使用相同的 url,我尝试使用的 api 正在使用 rest
    猜你喜欢
    • 2020-03-09
    • 2017-04-08
    • 2011-12-06
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多