ASP.NET Core IActionResult Unauthorized

I have an older ASP.NET Core Web API developed in Visual Studio 2017, targeting .NET Framework 4.5.2 and utilizing Microsoft.AspNetCore 1.1.2 and Microsoft.AspNetCore.Mvc 1.1.3.

In that project, I have a controller method that returns an IActionResult with lines like this, which work fine:

return Unauthorized();

return NotFound();

return Ok();

Open in new window


Similar to the discussion here:

https://stackoverflow.com/questions/37690114/how-to-return-a-specific-status-code-and-no-contents-from-controller



I am currently creating a new web API in VS 2017 targeting .NET Framework 4.6.1 and ASP.NET Core 2.0, using Microsoft.AspNetCore 2.0.1 and Microsoft.AspNetCore.Mvc 2.0.2.  In this new project, the same lines have a compilation error.

CS0103      The name 'Unauthorized' does not exist in the current context

VS provides recommendations to change these to:

UnauthorizedResult()

NotFoundResult()


Was this a breaking change made in Core 2.x?  Or am I missing a package or dependency in my new project?  If I use UnauthorizedResult(), I get an error that it cannot be used like a method.

If this is a change, I can't seem to find any documentation about it.
LVL 18
Steve EndowMicrosoft MVP - Dynamics GPAsked:
Who is Participating?
 
louisfrConnect With a Mentor Commented:
Can you check the namespace of the Controller class you're inheriting from?
0
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
Thanks for the response.  The namespace in the controller class appears to be correct--at least I don't see any issues.

As a test, I removed all of my controllers and added a new sample API controller.  Even that auto-generated sample controller is showing the same symptom.  Attached are two screen shots--one of my controller, and a second of a brand new controller based on the VS template.

I created a brand new empty Web API project targeting .NET Framework 4.6 and Core 2.0, and that seems fine--the controllers allow me to use return Unauthorized() and the other return types.

So it seems it is something wrong with my solution / project.  I copied all of the files and settings over from an old project, so my guess is that something I copied is breaking the Controller namespace.

I'll create a new project and migrate my old code one item at a time and see what breaks the controller.

Thanks,

Steve
Controller1.jpg
Controller2.jpg
0
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
Thanks, you nailed the cause.

I had migrated an old class that was called "Controller.cs" with a class name of Controller.  That broke references to the ASP.NET controller namespace.

Once I got rid of that class, the return values in my controllers work fine and the solution compiles.

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.