API Upgrade from .Net Framework to .Net 6.0 throwing 404 error in Postman

155 Views Asked by At

I have the following code which was working fine in .Net Framework but after upgrade it's throwing 404 error. This is basically my code :

[RoutePrefix("API")]
[BasicAuthenticationFilter]
public class APIController : ApiController
{
    [Route("Contact/New")]
    [HttpPost]
    public HttpResponseMessage NewContact(ContactAPIRequest request)
    {
        
    }

    [Route("Contact/Types")]
    [HttpGet]
    public HttpResponseMessage GetContactTypes()
    {
        
    }
}

I have it deployed on a website mangofruit.com(dummy name) and testing in Postman using https://mangofruit.com/API/Contact/New along with required authentication but I am getting a 404 error. I am also not able to access it using Visual Studio 2022.

Also https://mangofruit.com/API/Contact/Types throws an error 404. Am I missing something?

1

There are 1 best solutions below

0
Qing Guo On

In asp.net core 6 , Controllers in a web API are classes that derive from ControllerBase.

The web API project template like:

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase

You can try the below code:

   [ApiController]   
   [Route("[controller]")]   
    //[BasicAuthenticationFilter]
    public class APIController : ControllerBase
    {
        [Route("Contact/New")]
        [HttpPost]
        public HttpResponseMessage NewContact()
        {
           ...
        }

        [Route("Contact/Types")]
        [HttpGet]
        public HttpResponseMessage GetContactTypes()
        {
          ...//do your staff
           HttpResponseMessage response = null;// for test
           return response;
        }
    }

result: enter image description here

You can read Create web APIs with ASP.NET Core to know more.