![Creative The name of the picture]()

Clash Royale CLAN TAG#URR8PPP
ASP.NET Web API localhost:xxxx/api/product defaults to Home Page and not JSON data(orXML)
I am trying to pull data from SQLEXPRESS database via API to my client UWP app.
Here is my code(thus thinking):
API controller:
[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);
}
}
Model:
// 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;
}
}
}
And here is how client consumes(at least in accordance to the tutorial I followed:
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
}
The App.BaseUri is:
public static Uri BaseUri = new Uri("http://localhost:58834/api/Product/"); // base API URL; UserController
So, I must say this is my first attempt to use API.
Client originally was depending on SQLite that works like a charm. In later stages I decided to sync with MySQL. I managed to make it work within a client app.
However, I would like to move all the SQL related code into the API. The API would serve not only UWP app but also Android and iOS(Xamarin.Forms).
The API URL returns the ASP.NET Home Page. Clearly, the API does not manage to get the data from the database and no JSON data is available for the Client app.
What am I doing wrong>???
(As an addition - all the SQL related code works well from the client though)
1 Answer
1
I did several things that made my controller work:
1. Instead of renaming existing controller(the one that is created on project creation) I created new controller. This is important as the project creates new Views folder for each controller without which it would not work, and it does some other magic that makes controller work.
2. In Global.asax.cs file I reordered stuff so it is in this(correct) order:
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
3. In Web.config I added runAllManagedModulesForAllRequests="true" to the <modules/> within <system.webServer> .
runAllManagedModulesForAllRequests="true"
<modules/>
<system.webServer>
And that's it. API works and SQL data is being passed through HttpGet to the Client app.
It took me more than 12 hours to fix it.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Did you try to add a breakpoint in your get handler? Does it get hit? I doubt this has something to do with SQL but rather with your controller not being known to the application.
– derpirscher
yesterday