在客户端中更改Azure移动应用程序服务URL

时间:2016-04-19 10:09:23

标签: c# azure azure-mobile-services

我已升级后端服务以使用Azure Mobie App SDK并支持OData查询。我使用Postman提出请求并收到所需的回复。它工作正常。现在我想升级我的客户端以使用Azure Mobile App SDK。我面临的问题是客户端SDK在生成的URL中使用了硬编码的。这段代码:

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());
    }

1 个答案:

答案 0 :(得分:1)

使用IMobileServiceTable和IMobileServiceSyncTable,请求始终使用/ tables端点。要更改此行为,请将委派处理程序附加到MobileServiceClient,并在发送之前更改请求URI。

以下是执行日志记录的委派处理程序示例,其中显示了您添加此代码的位置:Log outgoing requests in mobile client