2017-03-16 20 views
0

私はこのメソッドを使用して、要求を別のコントローラにリダイレクトしようとしています。呼び出されたコントローラには、以前のコントローラからのデータが必要なので、これをTempdataに入れました。私はRedirectToActionを呼び出す行の前にTempDataをを使用する場合でも、ちょうどredirecttoactionを呼び出す前に、以下のコード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を設定しています内のすべての

枚次のアクションに渡され取得されていない要求コードが実行されたときにAuthorizationメソッドを呼び出すためにAuthorizationコントローラには向かない。

私は上記のコードの行のtempdataの部分にコメントしてみましたが、正常に動作していますが、私は承認コントローラにユーザデータが必要で、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の部分に行かないのかという理由の1つは、上記のコードで例外が発生したことです。 try catchブロックを置いてみて、上の行に例外が発生していないかどうか確認してください – Jinish

答えて

0

を私はセッションを設定するには、コアのウェブサイトからの指示に従ってきたし、そのすべては、以下のように細かいです。

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

オブジェクトを逆シリアル化して取得します。

var user = JsonConvert.DeserializeObject<AccountHolderDetails>(TempData["UserData"].ToString()); 
関連する問題