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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為 ASP.Net MVC 站點(diǎn)創(chuàng)建 customBaseController?

為 ASP.Net MVC 站點(diǎn)創(chuàng)建 customBaseController?

C#
UYOU 2021-08-29 17:56:45
在我正在處理的項(xiàng)目中,我發(fā)現(xiàn)在每個(gè)操作的開(kāi)始和結(jié)束時(shí),我都有相同的代碼來(lái)檢索,然后將對(duì)象存儲(chǔ)在 TempData[] 中。所有操作之間的代碼都是一致的,所以我想知道創(chuàng)建一個(gè)基本控制器類來(lái)執(zhí)行冗余重構(gòu)并將對(duì)象存儲(chǔ)在 TempData[] 中是否合理?有沒(méi)有更聰明的方法?我目前的代碼:public ActionResult Index(StepOne data){    var customer = TempData["customer"] as Customer;    //do stuff with customer    TempData["customer"] =customer;    return View();}
查看完整描述

1 回答

?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊

為什么不創(chuàng)建一個(gè)名為 BaseController 或其他控制器的控制器,然后像下面編寫的代碼一樣從這個(gè) BaseController 繼承其他控制器。


//Your base controller

public class BaseController : Controller

{

    //This will be executed after every action call on the controllers inherited from this BaseController.

    //You can use OnActionExecuting in case you want the execution before the actions execution in your other controllers.

    protected override void OnActionExecuted(ActionExecutedContext filterContext)

    {

        Controller controller = filterContext.Controller as Controller;

        if (controller != null)

        {

            var customer = controller.TempData["customer"] as Customer;


            //do stuff with customer


            controller.TempData["customer"] = customer;

        }

    }

}


//Then your other controller

public class HomeController : BaseController

{

    public ActionResult Index(StepOne data)

    {

        //You can get your TempData here too.

        var customer = TempData["customer"] as Customer;

        return View();

    }

}

如果它不起作用或者您需要將此代碼更改為某種其他類型的行為,請(qǐng)告訴我。


查看完整回答
反對(duì) 回復(fù) 2021-08-29
  • 1 回答
  • 0 關(guān)注
  • 176 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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