从 Xamarin.Forms 访问 Web API 的错误请求
在这里,我解释一下我的场景——我有 2 个项目 - API(存储 Web API)和 APP(存储 Xamarin.Forms).我已经使用 EDMX 从 API.Models 文件夹中的 API 项目中的数据库创建数据模型.我想从 APP 项目访问 API,因此在我的 APP 项目中创建了类似的数据模型类.当模型类中没有外键时,一切正常.一旦将外键添加到 API 项目中的任何模型类,使用 API 就会开始给我一个错误请求错误"
Here, I explain my scenario - I have 2 projects - API (storing the Web API) and APP (storing the Xamarin.Forms). I have used EDMX to create Data models from my database in the API project in API.Models folder. I want to access the API from the APP project and hence have created similar data model classes in my APP project. Things work fine when there are no Foreign Keys present in the model class. As soon as a Foreign Key is added to any of the model classes in API project, consuming the API starts giving me a "Bad Request error"
API.Models 代码
namespace API.Models
{
using System;
using System.Collections.Generic;
public partial class Lead
{
public int ID { get; set; }
public string LeadName { get; set; }
public Nullable<decimal> AssignedTo { get; set; }
public virtual User User1 { get; set; } //User is another entity in API.Models
}
}
APP.Models 代码
namespace APP.Models
{
using System;
using System.Collections.Generic;
public partial class Lead
{
public int ID { get; set; }
public string LeadName { get; set; }
public Nullable<decimal> AssignedTo { get; set; }
public virtual User User1 { get; set; } //User is another entity in APP.Models
}
}
API 代码
namespace APIProject.API
{
public class IssueLogController : ApiController
{
CRMEntities db = new CRMEntities();
[ResponseType(typeof(Lead))]
public async Task<IHttpActionResult> PostLead(Lead lead)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Lead.Add(lead);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (LeadExists(lead.ID))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = lead.ID }, lead);
}
}
}
Xamarin.Forms 代码
Lead lead = new Lead();
lead.LeadName = txtLead.Text.Trim();
var uri = Constants.URL + "Lead/PostLead/";
HttpClient client = new HttpClient(new NativeMessageHandler());
var s = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
var json = JsonConvert.SerializeObject(lead, s);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
await DisplayAlert("Message", "Your Lead has been recorded.", "OK");
}
有什么想法可以解决这个问题吗?
Any ideas to resolve this?
正如@Gerald 在上面的评论中提到的,这是我在 API 代码中为找出错误所做的 -
As mentioned by @Gerald in comments above, here is what I did in the API code to figure out the error -
if (!ModelState.IsValid)
{
StringBuilder sb = new StringBuilder();
foreach (var state in ModelState)
{
foreach (var error in state.Value.Errors)
{
sb.AppendLine(error.ErrorMessage);
}
}
t_app_issueLog.ID = 1;
t_app_issueLog.Problem = sb.ToString();
return CreatedAtRoute("DefaultApi", new { id = lead.ID }, lead);
//return BadRequest(ModelState);
}