如何在 Azure 移动服务上创建多个 POST 方法

本文关键字:创建 POST 方法 服务 Azure 移动 | 更新日期: 2023-09-27 18:32:39

我有一个带有多个控制器的Azure移动服务。我的一个控制器(TestSetController(有一些额外的方法来检查插入。

我需要解决的问题:TestSet 表有两种不同类型的测试集,一种用于本地团队,另一种用于现场团队。该表包含两者的数据,记录通过"TeamType"字段进行区分,该字段表示是本地团队插入了测试集还是现场团队插入了测试集。在任何插入中,我想检查是否存在由其他团队插入的类似测试集。我想比较测试集(如果找到(,如果测试集不同,则在同一表上进行其他插入/更新。

但是,我不断收到此错误:

Exception=System.InvalidOperationException: Multiple actions were found that match the request: 
PostTestSetDTO on type sbp_ctService.Controllers.TestSetController
CheckForDiscrepancy on type sbp_ctService.Controllers.TestSetController
CompareTestPointAttempts on type sbp_ctService.Controllers.TestSetController
   at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)
   at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)
   at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext(), Id=f07761ae-1be7-4f00-90b0-685dd0c108f3, Category='App.Request'

这是我的控制器:

    public class TestSetController : TableController<TestSetDTO>
    {
        private Context context;
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            context = new Context();
            DomainManager = new SimpleMappedEntityDomainManager<TestSetDTO, TestSet>(context, Request, Services, testset => testset.Id);
        }
        // GET tables/TestSet
        [QueryableExpand("TestPointAttempts")]
        public IQueryable<TestSetDTO> GetAllTestSetDTO()
        {
            return Query(); 
        }
        // GET tables/TestSet/48D68C86-6EA6-4C25-AA33-223FC9A27959
        public SingleResult<TestSetDTO> GetTestSetDTO(string id)
        {
            return Lookup(id);
        }
        // PATCH tables/TestSet/48D68C86-6EA6-4C25-AA33-223FC9A27959
        public Task<TestSetDTO> PatchTestSetDTO(string id, Delta<TestSetDTO> patch)
        {
             return UpdateAsync(id, patch);
        }
        // POST tables/TestSet/48D68C86-6EA6-4C25-AA33-223FC9A27959
        public async Task<IHttpActionResult> PostTestSetDTO(TestSetDTO item)
        {
            TestSet testSet = AutoMapper.Mapper.Map<TestSetDTO, TestSet>(item);
            this.CheckForDiscrepancy(testSet);
            TestSetDTO current = await InsertAsync(item);
            return CreatedAtRoute("Tables", new { id = current.Id }, current);
        }
        // DELETE tables/TestSet/48D68C86-6EA6-4C25-AA33-223FC9A27959
        public Task DeleteTestSetDTO(string id)
        {
             return DeleteAsync(id);
        }
        public TestSet CheckForDiscrepancy(TestSet sourceTestSet)
        {
            // Set the team type to search for opposite to the one being posted.
            string searchTeamType = null;
            if (sourceTestSet.TestTeamType == "D")
            {
                searchTeamType = "F";
            }
            if (sourceTestSet.TestTeamType == "F")
            {
                searchTeamType = "D";
            }
            var testSetTable = context.TestSets;
            TestSet foundTestSet = (from ts in testSetTable
                                    where ts.TileId == sourceTestSet.TileId && ts.ScenarioId == sourceTestSet.ScenarioId && ts.TestTeamType.StartsWith(searchTeamType)
                                    select ts).SingleOrDefault();
            // If no other test set was found from the opposing team then the test set is missing.
            // Else a testSet was found so continue with checks.
            if (foundTestSet == null)
            {
                sourceTestSet.DiscrepancyTypeId = DiscrepancyType.Missing.ToString();
            }
            else
            {
                var testPointAttemptTable = context.TestPointAttempts;
                // Get all of the associated TestPointAttempts for each testSet.
                sourceTestSet.TestPointAttempts = (from tpa in testPointAttemptTable
                                                   where tpa.TestSetId == sourceTestSet.Id
                                                   orderby tpa.TestAttemptNumber
                                                   select tpa).ToList<TestPointAttempt>();
                foundTestSet.TestPointAttempts = (from tpa in testPointAttemptTable
                                                  where tpa.TestSetId == foundTestSet.Id
                                                  orderby tpa.TestAttemptNumber
                                                  select tpa).ToList<TestPointAttempt>();
                bool matchingTestSets = CompareTestPointAttempts(sourceTestSet.TestPointAttempts, foundTestSet.TestPointAttempts);
                if (!matchingTestSets)
                {
                    sourceTestSet.DiscrepancyTypeId = DiscrepancyType.Discrepancy.ToString();
                    sourceTestSet.DiscrepancyTestSetId = foundTestSet.Id;
                }
            }
            return sourceTestSet;
        }
        public bool CompareTestPointAttempts(IEnumerable<TestPointAttempt> sourceTPAs, IEnumerable<TestPointAttempt> foundTPAs)
        {
            bool pass = false;
            // First check if the total number of testPointAttempts are the same
            if (sourceTPAs.Count() == foundTPAs.Count())
            {
                foreach (TestPointAttempt sTpa in sourceTPAs)
                {
                    bool foundMatch = false;
                    foreach (TestPointAttempt fTpa in foundTPAs)
                    {
                        if (sTpa.TestAttemptNumber == fTpa.TestAttemptNumber)
                        {
                            if (sTpa.TalkIn == fTpa.TalkIn && sTpa.TalkOut == fTpa.TalkOut)
                            {
                                foundMatch = true;
                            }
                        }
                    }
                    if (!foundMatch)
                    {
                        return pass;
                    }
                }
                // The foreach loop finished successfully meaning all matches were found
                pass = true;
            }
            return pass;
        }
        /// <summary>
        /// The type of discrepancy a TestSet can have.
        /// </summary>
        public enum DiscrepancyType
        {
            Discrepancy,
            Missing,
            None
        }
    }
}

我正在使用数据传输对象 (DTO( 在实体模型之间进行映射。任何帮助将不胜感激。我已经在StackOverflow上查看了一些不同的答案 ASP.NET 但它们都在谈论更新配置。路线。这适用于 Azure 移动服务,它可能与简单的 ASP.NET 网站有不同的要求。

如何在 Azure 移动服务上创建多个 POST 方法

这就像将这两种方法设为私有并将实际的 POST 方法保留为公共一样简单。 ASP.NET 只会为公共方法自动创建路由。