亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

方法調用方法

方法調用方法

C#
人到中年有點甜 2023-07-09 16:46:01
我有一些 ASP.NET MVC 方法,其中有很多共同點。所以我嘗試編寫一種通用方法來消除代碼重用,但沒有收到結果。你能表明問題出在哪里嗎?// 初始代碼:    public ActionResult Details(int? id) {                if (id == null)                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);                Product product = db.Products.Find(id);                if (product == null)                    return HttpNotFound();                             return View(product);            }[HttpGet]        public ActionResult Edit(int? id) {            if (id == null)                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);            Product product = db.Products.Find(id);            if (product == null)                return HttpNotFound();            return View(product);        }[HttpGet]        public ActionResult Delete (int? id) {            if (id == null)                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);            Product product = db.Products.Find(id);            if (product == null)                return HttpNotFound();            return View(product);        }// 最終代碼:public void Details(int? id) {            Common(id);        }[HttpGet]public void Edit(int? id) {            Common(id);        }[HttpGet]public void Delete (int? id) {            Common(id);        }public ActionResult Common(int? Id) {            if (Id == null)                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);            Product product = db.Products.Find(Id);            if (product == null)                return HttpNotFound();            return View(product);        }輸出時出現空白屏幕
查看完整描述

1 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

您無意或有意地將調用者更改為 return void。呼叫者的簽名不應更改。他們仍然應該返回ActionResults:


public ActionResult Details(int? id) {

    return Common(id);

}

[HttpGet]

public ActionResult Edit(int? id) {

    return Common(id);

}

[HttpGet]

public ActionResult Delete (int? id) {

    return Common(id);

}


查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 136 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號