第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何從 .NET CORE API 返回警告消息

我如何從 .NET CORE API 返回警告消息

C#
楊魅力 2023-09-16 14:58:55
我在客戶端使用 Angular。我在服務(wù)器端使用 .NET CORE WEB API,并使用 SQL Server 作為數(shù)據(jù)庫。我的 WEB API 有層次。業(yè)務(wù)邏輯層為我執(zhí)行數(shù)據(jù)庫 dml 操作,并且我能夠執(zhí)行 get 和 post http 客戶端操作。但我必須檢查業(yè)務(wù)邏輯層中的數(shù)據(jù),如果數(shù)據(jù)格式錯誤,我想在客戶端上顯示錯誤,例如“此電話號碼格式錯誤。請輸入正確的格式”。我正確執(zhí)行了 DML(INSERT、SELECT)操作。我需要返回錯誤消息的幫助,并且我想向用戶顯示此錯誤消息。這是 .NET Core Web API PersonController.cs    [Route("api/[controller]")]    [ApiController]    [EnableCors(origins: "http://example.com", headers: "*", methods: "*")]    public class PersonController : ControllerBase    {        FihristBLL bll = new FihristBLL();        // GET: api/Person        [HttpGet]        public List<Person> Get()        {            return bll.GetNumbers();        }        // POST: api/Person        [HttpPost]        public void Post([FromBody] Person person)        {            bll.AddNumber(person);        }    }}這是執(zhí)行數(shù)據(jù)庫操作的業(yè)務(wù)邏輯層    public class FihristBLL    {        public void AddNumber(Person person)        {                if (person.PhoneNumber.Substring(0, 1) != "5" || 0 % Convert.ToInt64(person.PhoneNumber) != 0)                {                // WRONG FORMAT                }                else                {                    // CORRECT FORMAT                          FihristDAL db = new FihristDAL();                    SqlConnection conn = db.OpenConnection();                    string command = "INSERT INTO Persons (PhoneNumber,FirstName,LastName) VALUES (@phoneNumber,@firstName,@lastName)";                    SqlCommand sqlCommand = db.CreateConnection(command);                    sqlCommand.Parameters.AddWithValue("@phoneNumber", person.PhoneNumber);                    sqlCommand.Parameters.AddWithValue("@firstName", person.FirstName);                    sqlCommand.Parameters.AddWithValue("@lastName", person.LastName);                    sqlCommand.ExecuteNonQuery();                    conn.Close();                }             }    }}操作完美,工作正常。但我需要返回錯誤消息,例如“您剛剛輸入了格式錯誤的電話號碼”。并在 Angular 上顯示此消息。
查看完整描述

4 回答

?
慕田峪7331174

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個贊

快速響應(yīng)是拋出一個ArgumentException帶有驗(yàn)證錯誤描述的錯誤,在控制器級別捕獲它并將其映射到錯誤響應(yīng)中,并以異常消息作為內(nèi)容:


var validPhoneNumber = //your validation logic stuff

if (!validPhoneNumber ){

? ?throw new ArgumentException("Invalid phone number");

}


[HttpPost]

public IActionResult Post([FromBody] Person person)

{

? ? try{

? ? ? bll.AddNumber(person);

? ? }catch(ArgumentException argumentEx){

? ? ? ?return BadRequest(new {Message = argumentEx.Message});

? ? }

? ? // A better approach would be to return the created resource at this point

? ? return this.NoContent();

}

在角度方面,錯誤有效負(fù)載應(yīng)該在 的實(shí)例中可用HttpResponseError。


查看完整回答
反對 回復(fù) 2023-09-16
?
catspeake

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個贊

您可以使用該模型并將其返回。


public class Response<T>

{

        public Response(bool defalut = false)

        {

            IsSuccess = defalut;

            Message = "";

        }


        public Response(string message)

        {

            IsSuccess = false;

            Message = message;

        }


        public bool IsSuccess { get; set; }

        public string Message { get; set; }

        public T Data { get; set; }

}


查看完整回答
反對 回復(fù) 2023-09-16
?
慕絲7291255

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個贊

我會嘗試將驗(yàn)證邏輯封裝在一個對象中,序列化該對象并將其發(fā)送到 Angular,然后在 Angular 中檢查一切是否正常,然后繼續(xù),否則顯示一些錯誤消息。



查看完整回答
反對 回復(fù) 2023-09-16
?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個贊

當(dāng)格式錯誤時,您可以拋出異常并簡單地返回HttpResponseMessage?,如下所示:

[HttpPost]

public void Post([FromBody] Person person)

{

? ? try?

? ? {

? ? ? ? bll.AddNumber(person);

? ? }

? ? // Replace with own exception

? ? catch (FormatException e)

? ? {

? ? ? ? return new HttpResponseMessage(HttpStatusCode.BadRequest)

? ? ? ? {

? ? ? ? ? ? ReasonPhrase = "Phone number is not correctly formatted"

? ? ? ? };

? ? }


? ? return new HttpResponseMessage(HttpStatusCode.OK);

}


查看完整回答
反對 回復(fù) 2023-09-16
  • 4 回答
  • 0 關(guān)注
  • 208 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號