首页 > 解决方案 > 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. &nbsp;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>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3282.0

                    </font>

            </body>
        </html>
        <!-- 
        [HttpException]: The controller for path &#39;/tables/Customer/10k&#39; 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 &lt;customErrors mode="Off"/&gt;. Consider using &lt;customErrors mode="On"/&gt; or &lt;customErrors mode="RemoteOnly"/&gt; in production environments.-->

标签: xamarinxamarin.formsazure-mobile-services

解决方案


我找到了解决方案,但遇到了另一个问题。

我在客户控制器顶部有这条线

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

推荐阅读