How to make one controller with web API and non web API actions on the same controller.
When I make this, I have a 404 error for the non web API action.
I have read in differents article that it's possible to mix with ASP.NET core , but does not work.
Return 404 for:
http://localhost:5000/board/index?boardID=1
[Route("api/[controller]")]
public class BoardController : Controller
{
private IDataContext dataContext;
public BoardController(IDataContext dataContext)
{
this.dataContext = dataContext;
}
public IActionResult Index(int boardID)
{
return View();
}
[HttpGet]
public IEnumerable<Board> GetAll()
{
}
}
Or
Return 404 for http://localhost:5000/api/board/
public class BoardController : Controller
{
private IDataContext dataContext;
public BoardController(IDataContext dataContext)
{
this.dataContext = dataContext;
}
public IActionResult Index(int boardID)
{
return View();
}
[HttpGet]
public IEnumerable<Board> GetAll()
{
}
}
I think I get my second solution and use http://localhost:5000/board/getall for API action
The main difference is: Web API is a service for any client, any devices, and MVC Controller only serve its client. The same because it is MVC platform.
Web API controller implements actions that handle GET, POST, PUT and DELETE verbs. Web API framework automatically maps the incoming request to an action based on the incoming requests' HTTP verb. MVC controller usually handles GET and POST requests but you can handle other verbs also.
The IActionResult return type is appropriate when multiple ActionResult return types are possible in an action. The ActionResult types represent various HTTP status codes. Any non-abstract class deriving from ActionResult qualifies as a valid return type.
You could use:
[Route("api/[controller]")]
public class BoardController : Controller
{
private IDataContext dataContext;
public BoardController(IDataContext dataContext)
{
this.dataContext = dataContext;
}
[Route("/[controller]/[action]")]
public IActionResult Index(int boardID)
{
return View();
}
[HttpGet]
public IEnumerable<Board> GetAll()
{
}
}
Putting a slash in front of the route overrides the controller-level route attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With