0

I've seen a lot of examples around doing ASP.NET (.NET Framework not .NET Core) MVC integration tests using a HttpServer hosting an ApiController, but it doesn't seem to be able to host/expose a normal Controller.

For example, the following setup works:

Controller

public class TestController : ApiController
{
    [HttpGet]
    public string Debug()
    {
        return "This is debug content";
    }
}

Test

var config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{action}");
var server = new HttpServer(config);
var client = new HttpClient(server);
var response = await client.GetAsync("http://whatever/test/debug");
response.EnsureSuccessStatusCode();

If I change my TestController to extend Controller instead of ApiController, the response becomes 404.

Am I missing something here or this has never been supported?

Leon Zhou
  • 633
  • 6
  • 20
  • 1
    Only ASP.NET Web API/SignalR can be self hosted (OWIN), but WinForms/MVC cannot. `ApiController` is exactly part of Web API, but not `Controller` for MVC. – Lex Li Sep 01 '21 at 00:26
  • Thanks @LexLi, should post this as an answer. – Leon Zhou Sep 01 '21 at 04:21
  • That only remains a comment. If you want an answer on how to enable integration testing you might check out https://stackoverflow.com/questions/10378966/self-hosting-asp-net-mvc – Lex Li Sep 01 '21 at 05:11

0 Answers0