Unable to cast object of type 'Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor' to type 'Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor'.
- Dominant language
- C#
- Stars
- 1k
- Forks
- 127
- PR merge metrics
- No merged PRs in 30d
Description
Since the version 7.0.0, I get an exception
Unable to cast object of type 'Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor' to type 'Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor'.
If I rollback to 4.0.2, everything seems fine.
I can see the method has changed, I don't know if it's an issue with the controllers configuration
```c#
// fails when accessing controller.ControllerContext.ActionDescriptor
internal static ActionResult ToActionResult(this ControllerBase controller, Ardalis.Result.IResult result)
{
IDictionary properties = controller.ControllerContext.ActionDescriptor.Properties;
ResultStatusOptions resultStatusOptions = (properties.ContainsKey((object) "ResultStatusMap") ? properties[(object) "ResultStatusMap"] as ResultStatusMap : new ResultStatusMap().AddDefaultMap())[result.Status];
int statusCode = (int) resultStatusOptions.GetStatusCode(controller.HttpContext.Request.Method);
return result.Status == ResultStatus.Ok ? (!typeof (Ardalis.Result.Result).IsInstanceOfType((object) result) ? (ActionResult) controller.StatusCode(statusCode, result.GetValue()) : (ActionResult) controller.StatusCode(statusCode)) : (!(resultStatusOptions.ResponseType == (Type) null) ? (ActionResult) controller.StatusCode(statusCode, resultStatusOptions.GetResponseObject(controller, result)) : (ActionResult) controller.StatusCode(statusCode));
}
```
```c#
// The property is being cast here
// Microsoft.AspNetCore.Mvc.ControlleContext
public new ControllerActionDescriptor ActionDescriptor
{
get { return (ControllerActionDescriptor)base.ActionDescriptor; }
set { base.ActionDescriptor = value; }
}
```
Contributor guide
Research direction
Start at the ToActionResult entry point and inspect how controller.ControllerContext.ActionDescriptor is obtained and cast to ControllerActionDescriptor. Compare the behavior between versions 7.0.0 and 4.0.2, then reproduce the exception and establish what configuration or compatibility change is required for the result mapping to work without the invalid cast.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100