Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am getting the above error .

"
C#
Unable to cast object of type 'System.Web.Mvc.RedirectToRouteResult' to type 'System.Web.Mvc.JsonResult'.

"

Testmethod:

C#
[Fact]
     public async void FindContractDetailsAsync_ByContractNo()
     {
         // Arrange
         Mock.Arrange(() => this.queryService.ExecuteAsync(Arg.IsAny<GetByIdQueryAsyc<Contract>>())).Returns(Task.FromResult(this.GetContract()));
         ContractController contractController = this.ContractController;

         // Act
         ActionResult result = await contractController.FindContractDetailsAsync();

         // Assert
         Assert.NotNull(result);
         Assert.Equal(expected: "ContractDetails", actual: ((ViewResult)result).ViewName);
         Assert.IsType(typeof(ViewResult), (ViewResult)result);
     }



Controller:

C#
[ActionName("ContractDetail")]
      public async Task<ActionResult> FindContractDetailsAsync()
      {
          try
          {
              ContractDetailsViewModel contractDetailsViewModel = default(ContractDetailsViewModel);
              var navigationViewModel = (NavigationViewModel)this.TempData["NavigationViewModel"];
              if (navigationViewModel != null)
              {
                  navigationViewModel = (NavigationViewModel)this.TempData["NavigationViewModel"];
                  contractDetailsViewModel = await this.ReadContractDetailsByContractNoAsync(navigationViewModel.Id).ConfigureAwait(continueOnCapturedContext: false);
              }

              if (contractDetailsViewModel == null)
              {
                  return this.RedirectToPageNotFound();
              }

              if (navigationViewModel.IsSuccess)
              {
                  contractDetailsViewModel.IsMessageDisplayed = navigationViewModel.IsMessageDisplayed;
                  contractDetailsViewModel.IsEdit = navigationViewModel.IsEdit;
                  navigationViewModel.IsMessageDisplayed = true;
              }

              this.TempData["NavigationViewModel"] = navigationViewModel;
              this.TempData.Keep();
              return this.View(viewName: "ContractDetails", model: contractDetailsViewModel);
          }
          catch (Exception ex)
          {
              return this.RedirectToAction(actionName: "Error", controllerName: "Error");
          }
      }


What I have tried:

Tried changing the ACtionResult to Viewresult but dint work
Posted
Comments
Sergey Alexandrovich Kryukov 8-Mar-16 10:57am    
What makes you thinking that they could ever cast? For a successful cast, during runtime, runtime type of your object should be assignment-compatible with the target type (even though its compile-time may not be compatible, being upper on the inheritance hierarchy; otherwise you would not need the cast). Your two types a peers, they will never cast to each other...

What you are trying to achieve?

—SA
Maciej Los 8-Mar-16 10:59am    
What line causes this error?
What's exact, more detailed message?
John C Rayan 8-Mar-16 11:15am    
This is the problem.
return this.RedirectToAction(actionName: "Error", controllerName: "Error");

Show us the code for Error Action in Error Controller.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900