2017-03-16 131 views
0

我有這種方法,我試圖重定向請求到不同的控制器。對於被調用的控制器,我需要前一個控制器的一些數據,所以我把它放在了Tempdata中。但是,當我在那裏我打電話RedirectToAction前行使用的TempData,請求在下面的代碼RedirectToAction不與Tempdata-ASP.net核心MVC

public IActionResult GetAuthToken() 
     { 
      //Logic to generate AuthToken 

      //TODO:SRI 14/03/2017 Get the UserId from the front end app, For now I am hardcoding this value 

      AccountHolderDetails user = new AccountHolderDetails 
      { 
       UserId = "123", 
       FirstName = "", 
       LastName = "", 
       Email = "", 
       Phone = "", 
       Address = new UserAddress 
       { 
        Street = "123 anystreet", 
        City = "Brantford", 
        State = "ON", 
        Country = "CA"//, 
            //PostalCode = "M4P1E8" 
       } 
      }; 

      var uniqueId = Guid.NewGuid(); 
      var authToken = GenerateAuthToken(user.UserId, uniqueId); 
      var transactionRecord = CreateTransactionRecord(user,uniqueId,authToken); 

      // HttpContext.Session.Set("UserData", user); 

      TempData["UserData"] = user; 
      TempData["TransactionData"] = transactionRecord; 

      return RedirectToAction("Authorize", "Authorization"); 

     } 

我設置到TempData不獲取傳遞到下一個動作都

像只調用redirecttoaction前,並且當代碼執行時,它不會去授權控制器去執行授權方法。

我試着評論上述代碼中的行的tempdata部分,它工作正常,但我需要授權控制器中的用戶數據,因此使用tempdata。我按照從核心的網站說明配置會話和所有的優良像下面看看我starttup.cs類

public void ConfigureServices(IServiceCollection services) 
     { 
      services.AddDistributedMemoryCache(); 
      services.AddSession(options => { 
       options.IdleTimeout = TimeSpan.FromSeconds(10); 
       options.CookieHttpOnly = true; 

      }); 

      // Add framework services. 
      services.AddMvc(); 

      services.Configure<WireCardSettings>(Configuration); 

      services.AddSingleton<ITempDataProvider, CookieTempDataProvider>(); 
     } 

     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
      loggerFactory.AddDebug(); 

      if (env.IsDevelopment()) 
      { 
       app.UseDeveloperExceptionPage(); 
       app.UseBrowserLink(); 
      } 
      else 
      { 
       app.UseExceptionHandler("/Home/Error"); 
      } 

      app.UseStaticFiles(); 

      app.UseSession(); 

      app.UseMvc(routes => 
      { 
       routes.MapRoute(
        name: "default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 
      }); 
     } 
    } 
+0

其中一個原因,可能不會去RedirectToAction部分原因是,上面的代碼中出現異常。嘗試放一個try catch塊,看看上面的任何一行是否會導致異常 – Jinish

回答

0

你必須將其分配給TempData的前序列化對象。

TempData["UserData"] = JsonConvert.SerializeObject(user); 

並通過反序列化來檢索對象。

var user = JsonConvert.DeserializeObject<AccountHolderDetails>(TempData["UserData"].ToString());