After upgrading to .Net 6, system.web no longer contains HttpException

Viewed 459

In old versions of .Net Core, I could do this to throw an exception:

public class MyParentController : Microsoft.AspNetCore.Mvc.Controller
{
    protected virtual GamePlayer CurrentPlayer => Players.GetPlayer(Session) ?? throw MyCustomHttpException(HttpStatusCode.Unauthorized, "Unauthorized");

    protected Exception MyCustomHttpException(HttpStatusCode code, String msg) => throw new System.Web.HttpException(code.ToString(), Int32.Parse(msg));
}

But after upgrading my solution to .Net 6, I am getting this error:

The type or namespace name HttpException does not exist in System.Web

Does new .Net not offer anything like this?

Thanks!

2 Answers

In your case you should do:

public class MyParentController : Microsoft.AspNetCore.Mvc.Controller
{
    [HttpGet("CurrentPlayer")]
    public IActionResult GetCurrentPlayer() 
    {
        var player = Players.GetPlayer(Session);
        return player != null ? Json(player) : Unauthorized();
    }
}
Related