我在Azure上运行了一个MobileService,并决定创建一个新的服务并自己迁移代码。新服务是一种新型号称为:Azure Mobile App Service。
目前,我的身份验证正在工作,并且可以进行迁移/更新数据库。我正在遵循TodoItem示例。现在,我想创建自己的自定义API,在MobileService上很容易工作,但我无法在Azure Mobile App上使其工作 :/
我已经按照这两个链接 web-Api-routing 和 app-service-mobile-backend 进行了跟随。现在我有以下内容:
我创建了一个新的控制器:
[MobileAppController]
public class TestController : ApiController
{
// GET api/Test
[Route("api/Test/completeAll")]
[HttpPost]
public async Task<ihttpactionresult> completeAll(string info)
{
return Ok(info + info + info);
}
}
在mobileApp.cs文件中,我根据后端文档添加了以下代码:
HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
另外,根据 web-api-routing,我已经安装了以下软件包:
Microsoft.AspNet.WebApi.WebHost
并且客户端的请求:
string t = await App.MobileService.InvokeApiAsync<string,string>("Test/completeAll", "hej");
调试显示,这是正确的URL:
{方法:POST,请求URI:'https://xxxxxxx.azurewebsites.net/api/Test/completeAll', 版本:1.1,内容:System.Net.Http.StringContent,头文件:{X-ZUMO-FEATURES: AT X-ZUMO-INSTALLATION-ID: e9b359df-d15e-4119-a4ad-afe3031d8cd5 X-ZUMO-AUTH: xxxxxxxxxxx Accept: application/json User-Agent: ZUMO/2.0 User-Agent: (lang=Managed; os=Windows Store; os_version=--; arch=Neutral; version=2.0.31125.0) X-ZUMO-VERSION: ZUMO/2.0 (lang=Managed; os=Windows Store; os_version=--; arch=Neutral; version=2.0.31125.0) ZUMO-API-VERSION: 2.0.0 Content-Type: application/json; charset=utf-8 Content-Length: 3}}
但是一直收到404(未找到)错误。
我错过了什么吗 :/ ?
更新
我尝试在mobileApp.cs中扩展代码:
HttpConfiguration config = new HttpConfiguration();
new MobileAppConfiguration()
.UseDefaultConfiguration().MapApiControllers()
.ApplyTo(config);
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
基于应用服务后端,但仍然无法访问 :/。
更新
我使用fiddler2通过浏览器访问终结点并获得以下结果:
再次更新
我尝试创建另一个最小化的解决方案,但仍然遇到相同的错误。是否有任何很好的教程可以供我遵循以实现此功能?
积极的感觉正在逐渐消失...
该问题现在也在msdn上运行,如果有任何信息显示,我将在此处进行更新。
更新
测试了Linda的评论,事实上我可以访问值转换器:
// Use the MobileAppController attribute for each ApiController you want to use
// from your mobile clients
[MobileAppController]
public class ValuesController : ApiController
{
// GET api/values
public string Get()
{
MobileAppSettingsDictionary settings = this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings();
ITraceWriter traceWriter = this.Configuration.Services.GetTraceWriter();
string host = settings.HostName ?? "localhost";
string greeting = "Hello from " + host;
traceWriter.Info(greeting);
return greeting;
}
// POST api/values
public string Post()
{
return "Hello World!";
}
}
我可以使用post和get函数访问这个内容:
string t = await App.MobileService.InvokeApiAsync<string, string>("values", null, HttpMethod.Post, null);
或者
string t = await App.MobileService.InvokeApiAsync<string, string>("values", null, HttpMethod.Get, null);
但是,我贴的代码没有路由,为什么可以使用值访问它?如果不使用路由参数,原始控制器的路径会是什么?
额外信息 我现在已经向Microsoft创建了一个支持工单,并将更新其他信息.... 希望如此。 更新 来自MSDN论坛的信息:尝试使用MS_SkipVersionCheck 在这里阅读有关该属性的信息,似乎不适用。但是我尝试了它。对于我的API仍然是“未找到”,但原始API仍然有效。因此,它对此问题没有影响。