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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

louisfrCommented:
Can you check the namespace of the Controller class you're inheriting from?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.