xamarin - Azure 移动服务 UpdateAsync 给我 404 错误
问题描述
我正在研究 xamarin 表单并使用 azure 移动服务。当我尝试更新记录时,UpdateAsync 不起作用并给我一个 404 错误。
在调试时,我注意到调试器中的以下行:
<b> Requested URL: </b>/tables/Customer/10k<br><br>
10k是身份证。
当我通过上面的链接使用邮递员时,它给了我同样的 404 错误。但是,如果我使用该链接/tables/Customer?id=10k
,它可以在邮递员中使用。
这是来自 VS 的错误消息。
Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: <!DOCTYPE html>
<html>
<head>
<title>The resource cannot be found.</title>
<meta name="viewport" content="width=device-width" />
</head>
<body bgcolor="white">
<span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
<h2> <i>The resource cannot be found.</i> </h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
<br><br>
<b> Requested URL: </b>/tables/Customer/10k<br><br>
<hr width=100% size=1 color=silver>
<b>Version Information:</b> Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3282.0
</font>
</body>
</html>
<!--
[HttpException]: The controller for path '/tables/Customer/10k' was not found or does not implement IController.
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
--><!--
This error page might contain sensitive information because ASP.NET is configured to show verbose error messages using <customErrors mode="Off"/>. Consider using <customErrors mode="On"/> or <customErrors mode="RemoteOnly"/> in production environments.-->
解决方案
我找到了解决方案,但遇到了另一个问题。
我在客户控制器顶部有这条线
[Route("tables/Customer")]
我删除它并编辑
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
对此
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "tables/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
但是现在当我从客户端更新时,它给了我错误的请求错误。
更新
我修复了错误的请求错误。在我的后端客户类中,我有约会时间?在我的客户客户类中,我有日期时间..我删除了?.
现在我收到内部服务器错误。
更新 2
在我的数据库中,我试图更新的记录有一些 NULL 列,我填满了它们,现在我又收到了错误的请求。
更新 3
我让它工作了。我在数据库中有两个主键。当我调用 UpdateAsync 时,我只在对象中传递了一个键。当我通过这两个键时,它起作用了。
Customer customer = new Customer
{
Id="10k", // first primary key
CustomerId=12, // second primary key
FirstName = FirstNamelbl.Text.ToString(),
LastName = LastNamelbl.Text.ToString(),
DOB = doblbl.Date,
Email = Emaillbl.Text.ToString(),
Sex = GenderGroup.SelectedItem.ToString()
};
// save customer input
await manager.UpdateCustomerRecordAsync(customer);
管理者
public async Task UpdateCustomerRecordAsync(Customer customer)
{
try
{
await customerTable.UpdateAsync(customer);
}
catch (MobileServiceInvalidOperationException msioe)
{
Debug.WriteLine("Invalid sync operation: {0}", new[] { msioe.Message });
}
catch (Exception e)
{
Debug.WriteLine("Sync error: {0}", new[] { e.Message });
}
}
推荐阅读
- regex - 如果子字符串出现在 CSV 中一行的任何位置,如何将列设置为一个值?
- architecture - 微服务中的 grpc 组织
- python - 如何在python中使用来自requirements.txt的导入
- c - 无法理解 printf 在此程序中的工作原理
- java - 如何从java中的这个try catch函数返回一个值?
- python - 将 2 个数据框列与同一行中的 2 个 numpy 数组值进行比较
- html - 用颜色填充云或任何形式(动画)
- kubernetes - 使用作业激活数千个 Pod 的 Kubernetes 批处理性能
- javascript - 如何隐藏和显示 html 页面以制作简单的菜单?
- javascript - 如何访问变量键并将其用作另一个函数中的变量?