C# ASP.NET Web API localhost:xxxx/API/product默认为主页,而不是JSON数据(或XML)
我正试图通过API将数据从SQLEXPRESS数据库拉到我的客户端UWP应用程序 以下是我的代码(因此思考): API控制器:C# ASP.NET Web API localhost:xxxx/API/product默认为主页,而不是JSON数据(或XML),c#,mysql,json,asp.net-web-api,sql-server-express,C#,Mysql,Json,Asp.net Web Api,Sql Server Express,我正试图通过API将数据从SQLEXPRESS数据库拉到我的客户端UWP应用程序 以下是我的代码(因此思考): API控制器: [Route("api/product")] public class ProductController : ApiController { // GET: api/product [HttpGet] public ObservableCollection<Product> Get() { string
[Route("api/product")]
public class ProductController : ApiController
{
// GET: api/product
[HttpGet]
public ObservableCollection<Product> Get()
{
string userId = "newuser"; //for testing only
return Model.ReadAllSQLProducts(userId);
}
}
所以,我必须说这是我第一次尝试使用API。
客户端最初依赖于SQLite,它的工作方式很有魅力。在以后的阶段中,我决定与MySQL同步。我设法让它在客户端应用程序中工作。
但是,我想将所有与SQL相关的代码都移动到API中。该API将不仅服务于UWP应用程序,还服务于Android和iOS(Xamarin.Forms)
API URL返回ASP.NET主页。显然,API无法从数据库中获取数据,客户端应用程序也没有可用的JSON数据
我做错了什么
(作为补充-所有与SQL相关的代码都可以从客户端正常工作)我做了几件事,使我的控制器正常工作: 1.我没有重命名现有控制器(在项目创建时创建的控制器),而是创建了新控制器。这一点很重要,因为项目会为每个控制器创建新的视图文件夹,如果没有这些文件夹,它将无法工作,并且它会执行其他一些使控制器工作的魔法。 2.在Global.asax.cs文件中,我对内容进行了重新排序,因此其顺序如下(正确): 三,。在Web.config中,我向
中的
添加了runAllManagedModulesForAllRequests=“true”
就这样。API起作用,SQL数据通过HttpGet传递到客户端应用程序
我花了12个多小时来修复它。是否尝试在get处理程序中添加断点?它被击中了吗?我怀疑这与SQL有关,而是与应用程序不知道您的控制器有关。此外,为什么要使用可观察的集合作为API端点的结果。这对我来说似乎没有意义,因为你不需要在更改集合时触发任何事件。在开始执行API之前,我在应用程序中使用了ObservableCollection。“所以我把它原封不动地保留了下来。”derpirscher关于你的第一个评论,我必须核实你的意见suggestion@derpirscher我放置了断点,但代码没有到达处理程序。。。
// R part of CRUD
public static ObservableCollection<Product> ReadAllSQLProducts(string userId)
{
var connString = "Server=localhost\\SQLEXPRESS;Integrated Security = SSPI; database = myDbName";
string cmdText = "Select * FROM Product WHERE CONVERT(VARCHAR, UserId) = @userId;";
using (var sqlConnection = new SqlConnection(connString))
{
using (var sqlCmd = new SqlCommand(cmdText, sqlConnection))
{
List<Product> SQLProductList = new List<Product>();
sqlConnection.Open();
SqlParameter param = new SqlParameter();
param.ParameterName = "@userId";
param.Value = userId;
sqlCmd.Parameters.Add(param);
SqlDataReader reader = sqlCmd.ExecuteReader();
while (reader.Read())
{
Product sqlProduct = new Product();
sqlProduct.UserId = (string)reader["UserId"];
sqlProduct.Name = (string)reader["Name"];
sqlProduct.Category = (string)reader["Category"];
SQLProductList.Add(sqlProduct);
}
List<Product> myCollection = SQLProductList.ToList();
ObservableCollection<Product> dbSQLProductsList = new ObservableCollection<Product>(myCollection);
return dbSQLProductsList;
}
}
}
using (var client = new HttpClient())
{
var response = "";
Task task = Task.Run(async () =>
{
response = await client.GetStringAsync(App.BaseUri); // sends GET request
});
task.Wait(); // Wait
listViewAPI.ItemsSource = JsonConvert.DeserializeObject<ObservableCollection<Product>>(response); // Bind the list
}
public static Uri BaseUri = new Uri("http://localhost:58834/api/Product/"); // base API URL; UserController
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);