1

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!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185

2 Answers2

0

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();
    }
}
  • 1
    That doesn't solve the issue. The MyCustomHttpException can be used by children to throw other exceptions besides "Unauthorized", and your solution does not provide an answer on how to replace HttpException for generic cases. – Fábio Aug 29 '22 at 12:36
-1

HttpException Class just exist in .net framework ,I suppose your project was an ASP.Net project?

https://learn.microsoft.com/en-us/dotnet/api/system.web.httpexception?view=netframework-4.8

To Handle error in .net 6.0,I think you could read the document:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-6.0

Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11