Ниже приведен код для извлечения данных из хранилища таблиц Azure (AzureTableHelper.cs) и отображения в формате JSON.
Не могли бы вы сказать мне правильный формат URI для pid
, sid
и top
для приведенного ниже кода?
Как и предполагалось, я попробовал как http://localhost:51904/api/tracks/, что дает мне пустые результаты. Может кто-нибудь помочь мне написать этот URL?
Много Такс заранее
ValuesController.cs
using AzureREST.Models;
using System.Web.Http;
using System;
using Microsoft.WindowsAzure.Storage.Table;
using System.Collections.Generic;
using System.Linq;
namespace AzureREST.Controllers
{
public class TracksController : ApiController
{
AzService AzService = new AzService();
private CloudTable Wndashboard;
public TracksController()
{
Wndashboard = AzService.azManager.Wndashboard;
}
// GET api/values
public IEnumerable<AzManager.Rc522Entity> Get()
{
string pid = "";
string sid = "";
int rows = 10;
var nvc = System.Web.HttpUtility.ParseQueryString(this.Request.RequestUri.OriginalString);
IList<AzManager.Rc522Entity> rs = new List<AzManager.Rc522Entity>();
foreach (var item in nvc.AllKeys)
{
if (item != null)
{
if (item.Contains("pid"))
{
if (!string.IsNullOrWhiteSpace(nvc[item]))
{
pid = nvc[item];
TableQuery<AzManager.Rc522Entity> query = new TableQuery<AzManager.Rc522Entity>()
.Where(TableQuery.GenerateFilterCondition("ProductionOrder", QueryComparisons.Equal, pid));
var retrieved = Wndashboard.ExecuteQuerySegmentedAsync(query, null);
if(retrieved.Result.Count()>0)
rs.Add(retrieved.Result.Results[0]);
}
}
else if (item.Contains("sid"))
{
if (!string.IsNullOrWhiteSpace(pid))
{
if (!string.IsNullOrWhiteSpace(nvc[item]))
{
sid = nvc[item];
TableQuery<AzManager.Rc522Entity> query = new TableQuery<AzManager.Rc522Entity>()
.Where(TableQuery.GenerateFilterCondition("SerialNumber", QueryComparisons.Equal, sid));
var retrieved = Wndashboard.ExecuteQuerySegmentedAsync(query, null);
if (retrieved.Result.Count() > 0)
rs.Add(retrieved.Result.Results[0]);
}
}
}
else if (item.Contains("top"))
{
if (!string.IsNullOrWhiteSpace(nvc[item]))
{
rows = Convert.ToInt32(nvc[item]);
TableQuery<AzManager.Rc522Entity> query = new TableQuery<AzManager.Rc522Entity>();
var retrieved = Wndashboard.ExecuteQuerySegmentedAsync(query, null);
rs = retrieved.Result.Results.OrderByDescending(x => x.Timestamp).Take(rows).ToList();
}
}
}
}
return rs;
}
}
}
WebApiConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace AzureREST
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
RouteConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace AzureREST
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
//HomeCOntroller.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AzureREST.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Title = "Home Page";
return View();
}
}
}
[! [
//Azure TableHelper.cs
using System;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
namespace AzureREST.Models
{
public class AzManager
{
private class TableManager
{
CloudStorageAccount storageAccount;
CloudTableClient tableClient;
public TableManager(string connectionString)
{
storageAccount = CloudStorageAccount.Parse(connectionString);
tableClient = storageAccount.CreateCloudTableClient();
}
public CloudTable tableById(string id)
{
CloudTable table = tableClient.GetTableReference(id);
table.CreateIfNotExistsAsync();
return table;
}
}
public class Rc522Entity:TableEntity
{
public Rc522Entity(string productionOrder, string serialNumber)
{
this.PartitionKey = productionOrder;
this.RowKey = serialNumber;
}
public Rc522Entity()
{
}
//public string PartitionKey { get; set; }
//public string RowKey { get; set; }
public string ProductionOrder { get; set; }
public string SerialNumber { get; set; }
}
private TableManager tableManager;
public CloudTable Wndashboard;
public AzManager(string connectionString)
{
tableManager = new TableManager(connectionString);
Wndashboard = tableManager.tableById("Wndashboard");
}
}
public class AzService
{
static string storageAccount = "";
static string skey = "";
static string host = @"https://" + storageAccount + ".table.core.windows.net/";
static string tableName = "Wndashboard";
static string resource = string.Format(@"{0}", tableName);
static string uri = host + resource;
CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials(storageAccount, skey), true);
public AzManager azManager = null;
public AzService()
{
azManager = new AzManager("DefaultEndpointsProtocol=https;AccountName="+ storageAccount + ";AccountKey="+skey+";EndpointSuffix=core.windows.net");
}
}
}