2014-12-02 53 views
0

我有一個Azure移動服務與多個控制器。我的一個控制器(TestSetController)有一些額外的方法來檢查插入。如何在Azure移動服務上創建多個POST方法?

問題我需要解決的問題:TestSet表有兩種不同類型的TestSets,一個用於本地團隊,另一個用於實地團隊。該表包含兩者的數據,並且記錄通過「TeamType」字段進行區分,該字段表示本地團隊是否插入TestSet或現場團隊。在任何插入我想檢查是否存在由其他團隊插入的類似TestSet。我想比較TestSets(如果找到),然後在同一個表上做一些其他插入/更新,如果TestSets不同。

不過,我不斷收到此錯誤:

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 for ASP.NET的一些不同的答案,但他們都討論了更新config.Routes。這對於Azure移動服務來說可能與簡單的ASP.NET網站有不同的要求。

回答

0

就像將這兩種方法設爲私有並將實際的POST方法保持爲公共一樣簡單。 ASP.NET將只爲公共方法自動創建路由。