如何从API调用中填充GridView?
我是一个初学者学习ASP.NET,并且正在研究一个项目,我需要创建Web API以从SQL Server获取数据,然后从Web表单中调用该API以填充GridView表。
目前,我已经获得了Web API正确获取SQL表并将其“发送”到http:// localhost:58749/api/Movie
这是我的Web API模型和控制器类的代码:
namespace MovieAPI.Models
{
using System;
using System.Collections.Generic;
public partial class tblMovie
{
public int MovieID { get; set; }
public string MovieTitle { get; set; }
public string MovieRating { get; set; }
public int ReleaseYear { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MovieAPI.Models;
using System.Data;
using System.Data.SqlClient;
namespace MovieAPI.Controllers
{
public class MovieController : ApiController
{
public IHttpActionResult getMovieDetails()
{
MoviesDBEntities entities = new MoviesDBEntities();
var results = entities.tblMovies.ToList();
return Ok(results);
}
}
}
当我在Visual Studio中按Play按钮,然后导航到适当的URL时,输出就是这样:
[{"MovieID":1,"MovieTitle":"From Dusk Til Dawn","MovieRating":"R ","ReleaseYear":1996},{"MovieID":2,"MovieTitle":"Neighbors","MovieRating":"R ","ReleaseYear":2014},{"MovieID":4,"MovieTitle":"Avengers: Infinity War","MovieRating":"PG-13","ReleaseYear":2018}]
我遇到的问题是如何使用HTTPCLCLEINT实际调用Web API。我使用的是httpclient,因为我相信这是项目中所要求的。我的Web表单后端看起来像这样:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using MovieAPI.Models;
namespace MovieWebForm
{
public partial class Movie : System.Web.UI.Page
{
static void Main()
{
RunAsync().Wait();
}
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:58749/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP GET
HttpResponseMessage response = await client.GetAsync("api/Movie");
if (response.IsSuccessStatusCode)
{
var movies = await response.Content.ReadAsAsync<tblMovie>();
// add to datagrid
}
}
}
}
}
这是Web表单前端:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Movie.aspx.cs" Inherits="MovieWebForm.Movie" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="DataGrid1" runat="server" >
<Columns>
<asp:BoundColumn HeaderText="MovieID" DataField="MovieID"> </asp:BoundColumn>
<asp:BoundColumn HeaderText="MovieRating" DataField="MovingRating"> </asp:BoundColumn>
<asp:BoundColumn HeaderText="MovieTitle" DataField="MovieTitle"> </asp:BoundColumn>
<asp:BoundColumn HeaderText="ReleaseYear" DataField="ReleaseYear"> </asp:BoundColumn>
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>
我拥有的httpclient代码来自文档示例。但是,我无法得到任何回应,尽管我有可能检查错误的位置。此外,我不确定如何以将表数据填充GridView的方式正确调用Web API。我认为,如果我知道如何正确调用Web API,我将使用的GridView大纲会工作。
有任何提示或建议吗?
编辑:我修改了HTTP可以与我在线看到的示例更加一致。另外,该项目指定的数据杂志,我认为这是GridView,但也许两个是不同的。
编辑2:更改输出部分,因为现在应该输出JSON而不是XML。还插入了我当前的Web表格前端。
I am a beginner learning ASP.NET and I am working on a project where I need to create a Web API to get data from an SQL Server and then call that API from a Web Form to populate a gridview table.
I've currently gotten the Web API to properly get the SQL table and 'send' it to http://localhost:58749/api/Movie
Here is the code for my Web API Model and Controller classes:
namespace MovieAPI.Models
{
using System;
using System.Collections.Generic;
public partial class tblMovie
{
public int MovieID { get; set; }
public string MovieTitle { get; set; }
public string MovieRating { get; set; }
public int ReleaseYear { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MovieAPI.Models;
using System.Data;
using System.Data.SqlClient;
namespace MovieAPI.Controllers
{
public class MovieController : ApiController
{
public IHttpActionResult getMovieDetails()
{
MoviesDBEntities entities = new MoviesDBEntities();
var results = entities.tblMovies.ToList();
return Ok(results);
}
}
}
When I press the play button in visual studio and then navigate to the appropriate URL, the output is something like this:
[{"MovieID":1,"MovieTitle":"From Dusk Til Dawn","MovieRating":"R ","ReleaseYear":1996},{"MovieID":2,"MovieTitle":"Neighbors","MovieRating":"R ","ReleaseYear":2014},{"MovieID":4,"MovieTitle":"Avengers: Infinity War","MovieRating":"PG-13","ReleaseYear":2018}]
The problem I am running into is how to actually call the web API using HttpClient. I'm using HttpClient because I believe it is what is called for in the project. My Web Form backend looks like this:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using MovieAPI.Models;
namespace MovieWebForm
{
public partial class Movie : System.Web.UI.Page
{
static void Main()
{
RunAsync().Wait();
}
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:58749/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP GET
HttpResponseMessage response = await client.GetAsync("api/Movie");
if (response.IsSuccessStatusCode)
{
var movies = await response.Content.ReadAsAsync<tblMovie>();
// add to datagrid
}
}
}
}
}
And here is the Web Form front end:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Movie.aspx.cs" Inherits="MovieWebForm.Movie" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="DataGrid1" runat="server" >
<Columns>
<asp:BoundColumn HeaderText="MovieID" DataField="MovieID"> </asp:BoundColumn>
<asp:BoundColumn HeaderText="MovieRating" DataField="MovingRating"> </asp:BoundColumn>
<asp:BoundColumn HeaderText="MovieTitle" DataField="MovieTitle"> </asp:BoundColumn>
<asp:BoundColumn HeaderText="ReleaseYear" DataField="ReleaseYear"> </asp:BoundColumn>
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>
The HttpClient code I have there is from the documentation example. I've been unable to get any response however, although it's possible I'm checking the wrong place. Furthermore, I'm not sure how to properly call the Web API in a way that would populate the gridview with the table data. I think the gridview outline I have would work if I knew how to properly call the Web API.
Any tips or suggestions?
EDIT: I modified the HTTP GET to be more in line with examples I see online. Also, the project specified Datagrid, which I assumed to be Gridview but perhaps the two are different.
EDIT 2: Changed the output section as it should now output JSON instead of XML. Also inserted my current Web Form front end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)