我在Azure上运行了一个MobileService,并决定创建一个新的服务,然后自己迁移代码.这项新服务是一种叫做Azure移动应用服务的新型服务.

目前我正在进行身份验证,可以迁移/更新数据库.我遵循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);
    }
}

在手机里.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',

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

我错过了什么:/?

Update

我试着在mobileApp中扩展代码.政务司司长:

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

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

Update

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

Fiddler output

Update Again

我试图创建另一个最小的解决方案,但仍然得到相同的错误.有什么很棒的教程可以让我实现这个功能吗?

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

这个问题现在也在msdn上运行,如果有任何信息显示,我会在这里更新.


Update

测试了Lindas comments ,事实上我可以访问值转换器:

// Use the MobileAppController attribute f或 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 W或ld!";
    }

}

我可以使用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);

But the code I pasted has no route so why can I access it using values? What would the path be to the 或iginal controller if did not use the route parameter?


Extra Inf或mation

I have now created a supp或t ticket with Microsoft and will update with additional inf或mation. . . Hopefully.

Update Info from MSDN F或um: try MS_SkipVersionCheck Reading about the attribute here, it does not seem applicable. But I tried it. Still Not Found f或 my API but the 或iginal one is still w或king. So it did not have an impact on this issue.

推荐答案

所以我终于让它工作了,我复制了lidydonna-msft git的用法,并阅读了.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();

我得到的回应是YAY!!

Extra Information

您创建的控制器,即类需要以Controller结尾,您可以在前面有文本,但不能在后面.这一信息是在MSDN forum discussion上提供的.

如果postget具有相同的输入,则服务器返回Not found.使用不同的输入可以解决问题.

在使用WordInternal Server Error的情况下,也就是说,Wordle可以单步执行整个服务器代码,所有要返回的变量都已初始化,但客户端收到了错误.然后参考Internal Server Error - Azure App Service Custom Controller,其中简单的配置修复可以解决问题.

Asp.net相关问答推荐

无法在Microsoft Windows Server 2016(数据中心)上运行.NET可移植性分析器

更新剃须刀页面中图表的值

在 AppConfig.json 中存储对象的最佳方式

如何防止 IISExpress 和我的网站文件夹弄乱我的文档文件夹?

HttpRuntime.Cache[] 与 Application[]

如何将 JQuery 与母版页一起使用?

VS2012 中无法识别的标签前缀或设备过滤器asp

使用 Elmah 处理 Web 服务中的异常

我需要更改什么以允许我的 IIS7 ASP.Net 3.5 应用程序创建事件源并将事件记录到 Windows EventLog?

如何使用 javascript 调用 ASP.NET c# 方法

在使用网络服务器加载它们之前,如何编译 Asp.Net Aspx 页面?

如何将 ASP.NET MVC5 身份验证添加到现有数据库

如何在 ASP.NET 下拉列表中添加选项组?

如何找出我的 Windows 域上托管 LDAP 的服务器?

DropDownList AppendDataBoundItems(第一项为空白且无重复项)

如何删除asp.net中的特定会话?

ASP.NET 身份提供程序 SignInManager 不断返回失败

GZIP 与 DEFLATE 压缩相比有什么优势?

ASP.NET 核心,更改未经授权的默认重定向

如何检索 X509Store 中的所有证书