【问题标题】:Azure Mobile Service Client InvokeApiAsync not hitting WebAPIAzure 移动服务客户端 InvokeApiAsync 未命中 WebAPI
【发布时间】:2023-03-27 23:51:01
【问题描述】:

我似乎无法从我的 xamarin 表单移动应用程序中访问我的 Web api。我可以通过 PostMan 访问 api,它工作正常。

输入文本时运行的命令

return _searchCommand ?? (_searchCommand = new Command<string>(async (text) => await ExecutSearchCommand(text)));

此命令调用我的 ExecuteSearchCommand 方法,我的客户端在该方法中调用我的 api

async Task ExecuteSearchCommand(string search){
    var table = await App.Client.InvokeApiAsync("api/Item", System.Net.Http.HttpMethod.Get, new Dictionary<string, string> (){ { "filter", search } });
    ...
}

我的 ItemController 看起来像这样

[MobileAppController]
[Route("api/Item")]
public class ItemController : ApiController
{
    [HttpGet]
    public async Task<IEnumerable<Item>> Get(string filter)
    {
        return await itemRepository.SearchAsync(filter);
    }
}

我在这里缺少什么吗?我尝试在本地运行时点击 web api,并将我的客户端指向 localhost 以及我托管的 web api。

*我确实为 android 项目设置了互联网权限

【问题讨论】:

  • 您将字典传递给方法,但您的方法需要字符串?
  • 根据我的阅读,我认为这就是您发送参数的方式

标签: asp.net-web-api xamarin.forms azure-mobile-services mobileserviceclient


【解决方案1】:

问题是这一行

var table = await App.Client.InvokeApiAsync("api/Item", System.Net.Http.HttpMethod.Get, new Dictionary<string, string> (){ { "filter", search } });

显然,它在使用 InvokeApiAsync 方法时已经添加了“api”前缀。删除这个解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    相关资源
    最近更新 更多