Azure的自定义控制器/ API后端的.Net [英] Azure Custom Controller / API .Net backend

查看:290
本文介绍了Azure的自定义控制器/ API后端的.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Azure上运行的MobileService,并已决定建立一个新的服务和迁移code自己。这项新服务是新类型称为:Azure的移动应用服务

I have had a MobileService running on Azure, and have decided to create a new service and migrate the code myself. The new service is of the new type called: Azure Mobile App Service.

目前我有工作的认证,并能做到迁移/更新数据库。我下面的例子内的TodoItem。我现在想创建自己的自定义API,这就容易在MobileService工作,但我不能让它在Azure移动应用工作:/

Currently I have Authentication working, and can do migrations/update-database. I am following the TodoItem example. I now want to create my own Custom API, which easily worked on MobileService, but I cannot get it working on Azure Mobile App :/

我按照这两个环节<一个href=\"http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2\">web-Api-routing和<一个href=\"https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-dotnet-backend-how-to-use-server-sdk/\">app-service-mobile-backend.我现在有以下几点:

I have followed these two links web-Api-routing and app-service-mobile-backend. And I now have the following:

我创建了一个新的控制器:

I have created a new controller:

[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我照着添加下面的code <一个href=\"https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-dotnet-backend-how-to-use-server-sdk/\">backend:

In the mobileApp.cs I have added the below code according to backend:

HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();

此外我还根据安装的软件包以下<一个href=\"http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2\">web-api-routing:

Additionally I have installed the below package according to web-api-routing:

Microsoft.AspNet.WebApi.WebHost 

和客户端的呼叫:

string t = await App.MobileService.InvokeApiAsync<string,string>("Test/completeAll", "hej");

调试表明,这是正确的网址:

Debug shows, that it is the correct URL:

{方法:POST,RequestUri:<一个href=\"https://xxxxxxx.azurewebsites.net/api/Test/completeAll\">https://xxxxxxx.azurewebsites.net/api/Test/completeAll',
  版本:1.1,内容:System.Net.Http.StringContent,头:{X-谟-特点:
  在x-谟安装-ID:e9b359df-d15e-4119-a4ad-afe3031d8cd5的X谟-AUTH:
  XXXXXXXXXXX接受:应用/ JSON的User-Agent:
  谟/ 2.0的User-Agent:(LANG =管理; OS = Windows应用商店; OS_VERSION = - ; ARCH =中性;版本= 2.0.31125.0)
    的X谟-VERSION:谟/ 2.0(LANG =管理; OS = Windows应用商店; OS_VERSION = - ; ARCH =中性;版本= 2.0.31125.0)
    谟-API的版本:2.0.0内容类型:应用程序/ JSON的;字符集= UTF-8的Content-Length:3}}

{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(未找到)
调试信息的请求无法完成。(未找到)

But keep getting: 404 (Not Found) Debug Message "The request could not be completed. (Not Found)"

我是什么缺少的:/?

更新

我曾尝试扩大code在mobileApp.cs,具有:

I have tried expanding the code in The mobileApp.cs, with:

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

根据<α

href=\"https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-dotnet-backend-how-to-use-server-sdk/\">app-service-backend,但仍然没有进入:/

based on app-service-backend, however still no access :/

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

I used fiddler2 to access the endpoint through a browser and got the following results:

再次更新

我试图创建另一个最小的解决方案,但仍然得到同样的错误。有没有我可以按照实现这一功能的任何伟大的教程?

I have tried to create another minimal solution, but still get the same error. Are there any great tutorials that I can follow to achieve this functionality?

在积极的感觉正在慢慢蒸发。 。 。

The positive feeling is slowly evaporating . . .

问题也对<一开始运行href=\"https://social.msdn.microsoft.com/Forums/azure/en-US/442c8c26-2a4d-4eed-b9f7-75debb31a1b6/azure-mobile-apps-custom-api-net-backend?forum=windowsazurewebsites$p$pview#442c8c26-2a4d-4eed-b9f7-75debb31a1b6\">msdn,如果显示有任何信息,我会在这里更新。

The question is also running now on msdn, I will update here if any information is shown there.

测试林多斯评论,我可以在实际上获得的价值转换器:

Tested Lindas comment, and I can in fact access the value converter:

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

}

使用这两个职位和获取函数这是我访问:

This I access using the both the post and get function:

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

不过,code我粘贴有没有路由,所以我为什么可以访问它使用值?将在路径是什么原始控制器如果没有使用路线参数

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

额外信息

我现在已经创建了微软的支持票,并与其他信息进行更新。 。 。但愿。

I have now created a support ticket with Microsoft and will update with additional information. . . Hopefully.

更新
从MSDN论坛信息:尽量MS_SkipVersionCheck
阅读有关该属性的here,它似乎并不适用。但是我试了一下。尽管如此未找到我的API,但原来仍正常工作。因此,它并没有在这个问题上产生影响。

Update Info from MSDN Forum: try MS_SkipVersionCheck Reading about the attribute here, it does not seem applicable. But I tried it. Still Not Found for my API but the original one is still working. So it did not have an impact on this issue.

推荐答案

是的!

所以,我终于得到它的工作,我复制从lidydonna的usings - MSFT 混帐和阅读关于<一个href=\"http://developers.de/blogs/damir_dobric/archive/2014/04/29/consuming-azure-mobile-service-custom-api-with-net-backend.aspx\"相对=nofollow>。净后端mobileservice 。

So I finally got it working, I copied the usings from 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随它的人。这似乎是它希望这两个函数 GET 。这导致在API被注册,并且可以存取。这意味着客户端调用我使用的服务器是:

This is a new controller and not the one that comes with it as lidydonna used. It seemed like it wants both functions get and post. This resulted in the API was registered and could be accessed. This means the client call to the server I used was:

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 !!

AND I GOT A RESPONSE YAY!!

额外信息

您所创建的控制器,即类需要用控制器结束,你可以有文本之前,但不是之后。这一信息是在<给定href=\"https://social.msdn.microsoft.com/Forums/azure/en-US/ea70204a-0f2d-4732-a00c-f3251a6089e0/azure-mobile-apps-custom-api-net-backend-second-controller?forum=azuremobile\"相对=nofollow> MSDN论坛讨论。

The controllers that you create, i.e. the class needs to end with Controller, you can have text before but not after. This information was given on a MSDN forum discussion.

这篇关于Azure的自定义控制器/ API后端的.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆