gpt4 book ai didi

c# - 如何将带有错误规范(无效用户名或无效密码)的 HttpResponseException 返回给前端?

转载 作者:太空宇宙 更新时间:2023-11-03 20:56:30 30 4
gpt4 key购买 nike

如何在 ApiController 中向前端返回带有“无效用户名”等文本的错误请求异常。我的代码:

public class FrontEndController : ApiController
{
public string Test(string value)
{
if(!isValid(value))
//how do i throw exception with its code (400 in this case) and message ("Invalid username")
}
}

最佳答案

您可以使用 HttpExceptionthis构造函数

throw new HttpException(400, "Invalid username")

关于c# - 如何将带有错误规范(无效用户名或无效密码)的 HttpResponseException 返回给前端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50029430/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com