简体   繁体   中英

Is there a way to throw a HttpException from codebehind in MVC asp.net core 2?

我正在尝试从MVC asp.net core 2 Web应用程序中的类中的代码中抛出一个简单的HttpException 404错误。

From documentation you could use StatusCodeResult as the result:

public StatusCodeResult StatusCodeActionResult()
{
    return StatusCode(404);
}

For more information see StatusCodeResult

I created a middleware to handle custom exceptions but I think that's not the best performance wise approach since core team says it's not a good practice to throw exception to handle business logic. You can see the link from OlafW comments

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