【发布时间】:2016-08-09 16:48:09
【问题描述】:
我正在尝试在我的 azure 移动服务客户端上调用自定义 api 控制器上的方法。
如果我在浏览器中点击路径,它会返回数据就好了。当尝试从我的应用程序调用它时,我收到以下错误
“Newtonsoft.Json.JsonReaderException:读取字符串时出错。意外标记:StartObject。路径'',第 1 行,位置 1。”
public async Task<string> AuthUser (string email, string pass)
{
var id = await client.InvokeApiAsync<string>(
"Login/AuthUser",
System.Net.Http.HttpMethod.Get,
new Dictionary<string, string>() {
{"emailAddress", email },
{"password",pass }
}
);
if (id != null)
{
return id.ToString();
}
else
{
return "";
}
}
这是我要调用的控制器
using System.Linq;
using System.Web.Http;
using System.Web.Http.Description;
using MyAppService.DataObjects;
using MyAppService.Models;
using Microsoft.Azure.Mobile.Server.Config;
namespace MyAppService.Controllers
{
[MobileAppController]
public class LoginController : ApiController
{
private MyAppContext db = new MyAppContext ();
[HttpGet]
[ActionName("AuthUser")]
public IHttpActionResult Login(string emailAddress, string password)
{
var login = db.Members.FirstOrDefault(m => m.Email == emailAddress && m.Password == password);
if (login != null)
{
return Ok(new {Id = login.Id });
}
else
{
return Unauthorized();
}
}
}
}
编辑:问题是控制器的返回类型。将其更改为字符串并且它有效。
【问题讨论】:
-
自定义 API 和 body 模型是什么样的?
-
发现问题。方法需要一个字符串,而我的服务返回一个 IHTTPActionResult。我的坏
标签: json azure azure-mobile-services