Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IActionResult in .NET Framework 4.6.1?

I have been searching around for an answer to this the last couple of hours without any success. I´m relatively new to programming so please go easy on me :-)

My problem is that it seems that I can´t use IActionResult in one of my projects in my solution which is targeting .NET Framework 4.6.1 but in the same solution (different project) targeting .NET Standard 2.0 I can use it.

I have tried to install exactly the same Nuget packages and everything looks exactly the same except for what framework that is targeted.

I just want to write Azure Functions using IActionResults like:

 public static async Task<IActionResult> MyFuntcion([HttpTrigger(AuthorizationLevel.Function, "get", Route = null")]HttpRequest req, TraceWriter log)
    {

      //Do some stuff then redirect

    }

I'm guessing that my targeted framework is whats causing me to not be able to use IActionResult, but I don't understand why.

Any help would be much appreciated, especially if one might know how to make this work.

Thank you

like image 901
jagge123 Avatar asked Jul 31 '18 11:07

jagge123


2 Answers

Short Version

You can't use an IActionResult in a Full Framework MVC or Web API as a result object. You'll have to map it to the appropriate type first. You should probably not use IActionResult in your class libraries, let the core project handle with return types

But ASP.NET Core doesn't require the .NET Core runtime, it can target the Full Framework as well. Perhaps you can use an ASP.NET Core project targeting the Full framework for your main project?

Explanation

ASP.NET Core is the "new" name for ASP.NET 5. It's a new framework that merges the MVC and Web API stacks and was built to run on both the older Full framework and what was then called DNXCore. Both names changed to ASP.NET Core 1.0 and Core .NET 1.0, leading to the confusion we have today.

Before ASP.NET Core, ASP.NET MVC and Web API were two different stacks, they used different controller base classes and returned different results. ASP.NET MVC used the abstract ActionResult class and Web API used the IHttpActionResult interface. Using an interface means it's a lot easier to test and mock the results, among other things. It also means that classes that implement it don't have to handle potentially unneeded properties and base methods.

In ASP.NET Core, the web framework, the entire stack was redesigned and now there's a single stack with a single base interface for results, IActionResult. Core's ActionResult is the default implementation but anyone can create a different implementation. RedirectResult for example doesn't need a Content property so it doesn't have one.

This interface is available to every .NET Standard 2.0 library as long as the appropriate package are added.

That doesn't mean it can be used as the actual return type in ASP.NET MVC 6 or Web API 2 projects though, because neither MVC's ActionResult nor Web API's IHttpActionResult implement it. One would have to write code to map from a concrete IActionResult instance to a concrete MVC ActionResult or Web API IHttpActionResult instance.

If your main project is ASP.NET Core though, you can use the interface whether it runs on the Full or Core framework.

.NET Standard

.NET Standard is a set of API specifications not a specific runtime. Each version specifies a set of classes and APIs that should be provided by a runtime that complies with that Standard version. When one creates a .NET Standard 2.0 library it means the code can use any of the classes and APIs defined in the 2.0 version.

Adding just the interface

IActionResult is not part of .NET Standard 2.0. It's provided by a package Microsoft.AspNetCore.Mvc.Abstractions.

like image 92
Panagiotis Kanavos Avatar answered Oct 24 '22 22:10

Panagiotis Kanavos


SOLUTION.

I needed to install Microsoft.AspNetCore.Mvc package in order to get it to work. Sry for my newbie questions and thank you very much for the help.

Edit: As @PanagiotisKanavos writes its enough with the Microsoft.AspNetCoreMvc.Abstractions package for IActionResult. Needed some other stuff beyond that so went for the whole package.

like image 34
jagge123 Avatar answered Oct 24 '22 23:10

jagge123