在客户端中更改 Azure 移动应用服务 url
Change Azure Mobile App service url in client
我已升级后端服务以使用 Azure Mobie App SDK 并支持 OData 查询。我使用 Postman 发出请求并收到所需的响应。它工作正常。现在我想升级我的客户端以使用 Azure Mobile App SDK。我面临的问题是客户端 SDK 在生成的 URL 中使用了硬编码的 tables。此代码:
class SiteRepository
{
private MobileServiceClient client;
private IMobileServiceSyncTable<Site> siteTable;
private SiteRepository()
{
client = new MobileServiceClient("http://{{host}}/");
Task.Run(() => InitLocalStoreAsync());
siteTable = client.GetSyncTable<Site>();
siteTable.PullAsync("Site", siteTable.CreateQuery());
}
private async Task InitLocalStoreAsync()
{
if (!client.SyncContext.IsInitialized)
{
var store = new MobileServiceSQLiteStore("localstore.db");
store.DefineTable<Site>();
await client.SyncContext.InitializeAsync(store);
}
}
public List<Site> GetSites()
{
return Task.Run(() => siteTable.ToListAsync()).Result;
}
发送请求至:
获取http://{{host}}/tables/site$filter=(updatedAt%20ge ...
但我希望它将请求发送至:
获取http://{{host}}/api/local/sites?$filter=(updatedAt%20ge...
我能否以某种方式将 tables
部分更改为其他内容?
更新:
这是后端的SiteController
:
[RoutePrefix("api/local/sites")]
[MobileAppController]
public class SiteController<T> : TableController<T> where T : Model.Entity
{
protected SiteService<T> service;
public BaseController(IEntityService<T> _service)
{
service = _service;
}
[Route("")]
[HttpGet]
[EnableQuery]
public virtual IQueryable<T> Get()
{
var entities = service.GetAll();
if (entities == null || entities.Count() == 0)
{
return null;
}
return TableUtils.ApplyDeletedFilter(entities.AsQueryable(), Request.AreDeletedRowsRequested());
}
对于 IMobileServiceTable 和 IMobileServiceSyncTable,请求始终使用 /tables 端点。要更改此行为,请将委托处理程序附加到您的 MobileServiceClient 并在发送之前更改请求 URI。
这是一个执行日志记录的委托处理程序的示例,其中显示了您要添加此代码的位置:Log outgoing requests in mobile client。
我已升级后端服务以使用 Azure Mobie App SDK 并支持 OData 查询。我使用 Postman 发出请求并收到所需的响应。它工作正常。现在我想升级我的客户端以使用 Azure Mobile App SDK。我面临的问题是客户端 SDK 在生成的 URL 中使用了硬编码的 tables。此代码:
class SiteRepository
{
private MobileServiceClient client;
private IMobileServiceSyncTable<Site> siteTable;
private SiteRepository()
{
client = new MobileServiceClient("http://{{host}}/");
Task.Run(() => InitLocalStoreAsync());
siteTable = client.GetSyncTable<Site>();
siteTable.PullAsync("Site", siteTable.CreateQuery());
}
private async Task InitLocalStoreAsync()
{
if (!client.SyncContext.IsInitialized)
{
var store = new MobileServiceSQLiteStore("localstore.db");
store.DefineTable<Site>();
await client.SyncContext.InitializeAsync(store);
}
}
public List<Site> GetSites()
{
return Task.Run(() => siteTable.ToListAsync()).Result;
}
发送请求至:
获取http://{{host}}/tables/site$filter=(updatedAt%20ge ...
但我希望它将请求发送至:
获取http://{{host}}/api/local/sites?$filter=(updatedAt%20ge...
我能否以某种方式将 tables
部分更改为其他内容?
更新:
这是后端的SiteController
:
[RoutePrefix("api/local/sites")]
[MobileAppController]
public class SiteController<T> : TableController<T> where T : Model.Entity
{
protected SiteService<T> service;
public BaseController(IEntityService<T> _service)
{
service = _service;
}
[Route("")]
[HttpGet]
[EnableQuery]
public virtual IQueryable<T> Get()
{
var entities = service.GetAll();
if (entities == null || entities.Count() == 0)
{
return null;
}
return TableUtils.ApplyDeletedFilter(entities.AsQueryable(), Request.AreDeletedRowsRequested());
}
对于 IMobileServiceTable 和 IMobileServiceSyncTable,请求始终使用 /tables 端点。要更改此行为,请将委托处理程序附加到您的 MobileServiceClient 并在发送之前更改请求 URI。
这是一个执行日志记录的委托处理程序的示例,其中显示了您要添加此代码的位置:Log outgoing requests in mobile client。