简体   繁体   中英

Mix web api action and non api action same controller asp.net core

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:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM