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

JTI*_*TIM 31 c# asp.net azure azure-mobile-services azure-api-apps

我在Azure上运行了一个MobileService,并决定创建一个新服务并自己迁移代码.新服务属于新类型:Azure Mobile App Service.

目前我有身份验证工作,可以进行迁移/更新数据库.我正在关注TodoItem示例.我现在想要创建自己的自定义API,它可以轻松地在MobileService上运行,但我无法在Azure Mobile App上运行它:/

我已经关注了这两个链接web-Api-routingapp-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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在mobileApp.cs中,我根据后端添加了以下代码:

HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
Run Code Online (Sandbox Code Playgroud)

另外我根据web-api-routing安装了以下软件包:

Microsoft.AspNet.WebApi.WebHost 
Run Code Online (Sandbox Code Playgroud)

和来自客户的电话:

string t = await App.MobileService.InvokeApiAsync<string,string>("Test/completeAll", "hej");
Run Code Online (Sandbox Code Playgroud)

调试显示,它是正确的URL:

{方法:POST,RequestUri:' 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接受: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内容长度:3}}

但继续得到:404(未找到)调试消息"请求无法完成.(未找到)"

我错过了什么:/?

更新

我尝试在mobileApp.cs中扩展代码,其中包括:

HttpConfiguration config = new HttpConfiguration();
        new MobileAppConfiguration()
            .UseDefaultConfiguration().MapApiControllers()
            .ApplyTo(config);
        config.MapHttpAttributeRoutes();
        app.UseWebApi(config);
Run Code Online (Sandbox Code Playgroud)

基于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!";
    }

}
Run Code Online (Sandbox Code Playgroud)

我使用post和get函数访问:

string t = await App.MobileService.InvokeApiAsync<string, string>("values", null, HttpMethod.Post, null);
Run Code Online (Sandbox Code Playgroud)

要么

string t = await App.MobileService.InvokeApiAsync<string, string>("values", null, HttpMethod.Get, null);
Run Code Online (Sandbox Code Playgroud)

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


额外的信息

我现在已经与Microsoft创建了一个支持服务单,并将更新其他信息...希望.

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

JTI*_*TIM 15

是的!!!

所以我终于让它工作了,我复制了lidydonna的使用 - msft git并阅读.net后端的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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个新的控制器,而不是像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();
Run Code Online (Sandbox Code Playgroud)

我得到了一个回应!

额外的信息

您创建的控制器,即类需要结束Controller,您可以在之前但不在之后拥有文本.此信息是在MSDN论坛讨论中提供的.

如果postget具有相同的输入,则服务器返回Not found.有不同的输入解决了这个问题.

如果是奇怪的Internal Server Error,即很奇怪,您可以单步执行整个服务器代码,初始化所有要返回的变量,但客户端会收到错误.然后参考内部服务器错误 - Azure应用服务自定义控制器,其中对配置的简单修复可以解决问题.


lin*_*nna 5

您的项目配置中必定有问题.我在这里有一个工作样本:https://gist.github.com/lindydonna/6fca7f689ee72ac9cd20

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

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