ASP.NET CORE 2.2 call to web api targeting .net framework does not get called

1.2k views Asked by At

I have a solution with contains a class library targeting the .Net framework 4.7 and another project with ASP.NET Core 2.2 .

The class library targeting the .Net framework contains controllers as shown below with the route prefix and the route name

enter image description here

And then in my StartUp.cs i have configured the below enter image description here

On running this i make a call to the api in the .Net framework class library the call does not return the response from the web api instead returns the default index.html content.

enter image description here What am i missing in this ?

2

There are 2 answers

2
AlbertK On BEST ANSWER

I see on the first screen classes like RoutePrefix, ApiController, HttpResponseMessage. They are from classic Asp.Net WebApi. It is impossible to use old ApiControllers in Asp.Net Core. It means you need to rewrite you controllers using Asp.net Core.

Asp.net core projects may targeting to both .net core and classic .Net Framework. If you really need to target to the .Net Framework you can create a class library targeting the .Net framework 4.7, create there Asp.Net Core controllers and then use that library in Asp.Net Core project as an external dll.

0
Chris Pratt On

You've got an error in your route. The correct way to mark a route param as optional is with a ?:

[Route("List/{cKey?}"]

The default, should be on the action param:

public HttpResponseMessage Get(string cKey = null)

Then, you can request just List and it will hit this action and cKey will be null, because it wasn't supplied. As you have it now, the cKey param is required, so you can only reach this route via List/foo, where "foo" is your cKey value.