0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.