1 回答

TA貢獻1884條經驗 獲得超4個贊
為什么不創建一個名為 BaseController 或其他控制器的控制器,然后像下面編寫的代碼一樣從這個 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();
}
}
如果它不起作用或者您需要將此代碼更改為某種其他類型的行為,請告訴我。
- 1 回答
- 0 關注
- 175 瀏覽
添加回答
舉報