Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

NullReferenceException thrown when Controller method uses Guid parameter default value

Open natewallace opened this issue 8 years ago • 2 comments

I'm using ASP.NET Core 2.0 with MVC and testing my app within Visual Studio 2017(15.3.0) using IIS Express. I'm also using version 7.1 of the C# language.

When I browse to a route that is associated with the following type of controller method using attribute routing:

[HttpGet]
public Task<IActionResult> List([FromQuery]Guid hookId = default)
{
     return Task.FromResult<IActionResult>(Ok());
}

I get the following exception

NullReferenceException: Object reference not set to an instance of an object.
  lambda_method(Closure , object , Object[] )
  Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(object target, Object[] parameters)
  Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeActionMethodAsync>d__12.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeNextActionFilterAsync>d__10.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
  Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
  Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeInnerFilterAsync>d__14.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResourceFilter>d__22.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeFilterPipelineAsync>d__17.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeAsync>d__15.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  Microsoft.AspNetCore.Builder.RouterMiddleware+<Invoke>d__4.MoveNext()
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

If I change the parameter of type Guid to be of type Guid? instead everything works as expected:

[HttpGet]
public Task<IActionResult> List([FromQuery]Guid? hookId = default)
{
     return Task.FromResult<IActionResult>(Ok());
}

I was expecting the routing to work with a default Guid parameter value or if it wasn't supported to throw a more meaningful exception message.

natewallace avatar Mar 21 '18 16:03 natewallace

Thanks for contacting us, @natewallace. @kichalla, can you please look into this? Thanks!

mkArtakMSFT avatar Mar 21 '18 16:03 mkArtakMSFT

I would like to resolve this issue. Added an action to the controller

        [HttpGet]
        public Task<IActionResult> List([FromQuery]Guid hookId=default)
        {
            return Task.FromResult<IActionResult>(Ok());
        }

but there is a compilation error

CS8107 Feature 'default literal' is not available in C# 7.0. Please use language version 7.1 or greater. MvcSandbox(net461), MvcSandbox(netcoreapp2.0) I have added the .csproj latest , but it does not work

How can I solve this problem?

tebrizetayi avatar Mar 27 '18 13:03 tebrizetayi