将操作方法​​的参数访问到控制器的构造函数中

时间:2015-10-14 10:48:41

标签: c# asp.net-mvc session

我有一个名为public function actionCreate() { $a=new A; $b=new B; if(isset($_POST['A'], $_POST['B'])) { // populate input data to $a and $b $a->attributes=$_POST['A']; $b->attributes=$_POST['B']; // validate BOTH $a and $b $valid=$a->validate(); $valid=$b->validate() && $valid; if($valid) { // use false parameter to disable validation $a->save(false); $b->save(false); // ...redirect to another page } } $this->render('create', array( 'a'=>$a, 'b'=>$b, )); 的控制器,它由所有其他控制器继承。我有一个NSString *business =self.business.name; NSArray *subscribedChannels = [PFInstallation currentInstallation].channels; for (NSString *channel in subscribedChannels){ NSLog(@"Channel %@ ",channel); } [currentInstallation addUniqueObject:business forKey:@"channels"]; NSLog(@"Business : %@",business ); [currentInstallation saveInBackground]; 的构造函数,我根据它从会话中选择的变量patId进行一些计算,并且根据此计算,用户可以执行某些操作。

但是有一个Controller BaseController,它的动作第一次从另一个应用程序模块接收到该模块的变量,所以直到那时它才被设置到会话中。

BaseController

因此我的MyController计算错误。有没有办法让这个变量值成为public ActionResult MyAction(string patId) { // First time patId is been set into session } 的构造函数。这样我就可以检查会话值是否为null,然后检查此参数并设置会话。

请建议我是否可以这样做,或者我可以通过任何其他方式解决我的问题。

1 个答案:

答案 0 :(得分:1)

Andrei评论帮助了我,我使用了thread mentioned by Andrei。讨论了拦截传入的请求。有两种方法可以帮助我HttpModuleApplication_BeginRequest,我选择了以后。

但是我在控制器本身的构造函数中通过使用HttpContext拦截http请求来实现它,这也是控制器的构造函数中可用的。

public BaseController()
{
   // Check if variable exist in session
   // else check if patId is sent through request
   // then save it in session

   System.Web.HttpContext.Current.Session["PatId"] = System.Web.HttpContext.Request.Form["patId"];

   // perform remaining calculation
}
相关问题