Azure 自定义控制器 / API .Net 后端

Azure Custom Controller / API .Net backend

我在 Azure 上有一个 MobileService 运行,并决定创建一个新服务并自己迁移代码。新服务属于名为:Azure 移动应用服务的新类型。

目前我有身份验证工作,并且可以 migrations/update-database。我正在关注 TodoItem 示例。我现在想创建自己的自定义 API,它很容易在 MobileService 上运行,但我无法让它在 Azure 移动应用程序上运行:/

我点击了这两个链接 web-Api-routing and 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 中,我根据 backend 添加了以下代码:

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:

{Method: POST, RequestUri: 'https://xxxxxxx.azurewebsites.net/api/Test/completeAll', Version: 1.1, Content: System.Net.Http.StringContent, Headers:{ 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(未找到) 调试消息 "The request could not be completed. (Not Found)"

我错过了什么:/ ?

更新

我尝试扩展 mobileApp.cs 中的代码,其中:

HttpConfiguration config = new HttpConfiguration();
        new MobileAppConfiguration()
            .UseDefaultConfiguration().MapApiControllers()
            .ApplyTo(config);
        config.MapHttpAttributeRoutes();
        app.UseWebApi(config);

基于 app-service-backend,但仍然无法访问:/

更新

我使用fiddler2通过浏览器访问端点,得到如下结果:

再次更新

我已经尝试创建另一个最小的解决方案,但仍然遇到同样的错误。有没有我可以遵循的很棒的教程来实现此功能?

积极的感觉正在慢慢消失。 . .

问题也在 运行 msdn,如果那里显示任何信息,我会在这里更新。


更新

测试了 Lindas 评论,实际上我可以访问值转换器:

// 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);

但是我粘贴的代码没有路由,为什么我可以使用值访问它?如果不使用route参数,原controller的路径是什么?


额外信息

我现在已经创建了 Microsoft 的支持票,并将更新更多信息。 . .希望。

更新 来自 MSDN 论坛的信息:尝试 MS_SkipVersionCheck 阅读属性 here,它似乎不适用。但我试过了。我的 API 仍然是 Not Found,但原来的仍然有效。所以对这个问题没有影响。

尝试将继承从 ApiController 切换到 TableController。

你的项目配置一定有问题。我这里有一个工作示例:https://gist.github.com/lindydonna/6fca7f689ee72ac9cd20

创建 HttpConfiguration 对象后,调用 config.MapHttpAttributeRoutes()。我添加了路由属性[Route("api/Test/completeAll")],我可以确认路由是否正确注册。

尝试将此属性添加到 ValuesController 并检查路由。

是的!!!

所以我终于让它工作了,我从 lidydonna - msft git and read about .net backend for mobileservice.

复制了使用方法

结尾如下:

using System.Web.Http;
using Microsoft.Azure.Mobile.Server.Config;
using System.Threading.Tasks;
using System.Web.Http.Tracing;
using Microsoft.Azure.Mobile.Server;

namespace BCMobileAppService.Controllers
{
[MobileAppController]
public class TestController : ApiController
{
    // GET api/Test
    [HttpGet, Route("api/Test/completeAll")]
    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
        [HttpPost, Route("api/Test/completeAll")]
        public string Post(string hej)
        {
            string retVal = "Hello World!" + hej;
            return retVal;
        }
    }
}

这是一个新的控制器,而不是 lidydonna 使用的那个控制器。看起来它需要 getpost 这两个函数。这导致 API 被注册并且可以被访问。这意味着客户端对我使用的服务器的调用是:

t = await App.MobileService.InvokeApiAsync<string, string>("Test/completeAll", null, HttpMethod.Post, new Dictionary<string, string>() { { "hej", " AWESOME !" }});

dialog = new MessageDialog(t);
dialog.Commands.Add(new UICommand("OK"));
await dialog.ShowAsync();

我得到了回复耶!!

额外信息

您创建的控制器,即 class 需要以 Controller 结尾,您可以在之前有文本但不能在之后。此信息是在 MSDN forum discussion.

上提供的

如果 postget 具有相同的输入服务器 returns Not found。使用不同的输入可以解决问题。

在奇怪的情况下 Internal Server Error,即奇怪你可以单步执行整个服务器代码,你想要 return 初始化的所有变量都被初始化,但客户端收到错误。然后参考,简单修改配置即可解决问题。

这真的很奇怪但是很简单 API 请求在 Azure 应用程序服务中不起作用 所以我想出了对我有用的解决方案。我已经用 c# http post/get、android post/get 和 objective C post/get 测试了 http 请求 所以首先你需要更新你的 Startup.MobileApp.cs class :

  new MobileAppConfiguration()
    .UseDefaultConfiguration()   
    .MapApiControllers()                /* /api endpoints **missing part***/ 
    .ApplyTo(config);

然后创建 Azure 移动应用自定义控制器。之后稍微修改你的控制器以获得正确的 json 响应

    public class Mes
    {
        public string message { get; set; }
    }

    // GET api/My
    public Mes Get()
    {

        return new Mes { message = "thanks" };


       // return "Hello from custom controller!";
    }
    // POST api/My
    public Mes Post(Mes chal)
    {

        return new Mes { message = chal.message + "asnwer" };


        // return "Hello from custom controller!";
    }
}

您可以简单地离开第一个变体并获得响应,但是 OBjective C 会告诉您 JSON 文本不是以数组或对象开头以及允许片段的选项。 ..等等.. 发生这种情况是因为您得到的是简单的字符串而不是对象。所以这就是为什么我用 class Mes 修改了我的回复 但这也取决于您提出请求的方式以及您期望的对象类型。 所以 .MapApiControllers() 它是 API 的主键,WEB API 控制器现在更改为 azure 自定义控制器。 希望这可以帮助。

我在使用属性路由时发现了 404 错误的另一个原因。

上面的代码最初在 mobileApp.cs:

HttpConfiguration config = new HttpConfiguration();
    new MobileAppConfiguration()
        .UseDefaultConfiguration().MapApiControllers()
        .ApplyTo(config);
    config.MapHttpAttributeRoutes();
    app.UseWebApi(config);

config.MapHttpAttributeRoutes() 需要移至 .ApplyTo:

之上
HttpConfiguration config = new HttpConfiguration();
 config.MapHttpAttributeRoutes();
 new MobileAppConfiguration()
        .UseDefaultConfiguration().MapApiControllers()
        .ApplyTo(config);