2017-09-12 22 views
0

従来のMVCコントローラと同様に、スタジオファイルを読み込むためのコードがRazorページにある方法はありますか?私は今朝、これを数時間プレイしてきましたが、これを達成する方法を見つけることができないようです。すべての入力をいただければ幸いです!Core 2 Razor Pages OnGetAsyncスタティックファイルを読み込む

の後ろ

カミソリページコード:

public async void OnGetAsync() 
    { 
     var request = HttpContext.Request; 
     var sessionId = request.Query.FirstOrDefault().Value; 
     var session = await _httpService.ValidateSession(sessionId); 

     if (!string.IsNullOrEmpty(session?.UserId)) 
     { 
      var claims = new List<Claim>() 
      { 
       new Claim(CustomClaimTypes.UserId, session.UserId), 
       new Claim(CustomClaimTypes.BuId, session.BuId), 
       new Claim(CustomClaimTypes.SecurityLevel, session.SecurityLevel) 
      }; 

      var identity = new ClaimsIdentity(claims, "TNReadyEVP"); 
      var principal = new ClaimsPrincipal(identity); 

      await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, 
       new AuthenticationProperties { ExpiresUtc = DateTime.Now.AddMinutes(60), IsPersistent = true, AllowRefresh = false }); 

      var isAuthenticated = principal.Identity.IsAuthenticated; 

      Redirect("~/wwwroot/index.html");## Heading ## 
     } 
     else 
     { 
      RedirectToPage("./Error"); 
     } 

MVCコントローラ

public async Task<IActionResult> SignIn() 
    { 
     var sessionId = HttpContext.Request.Query.FirstOrDefault().Value; 
     var session = await _httpService.ValidateSession(sessionId); 

     if (!string.IsNullOrEmpty(session?.UserId)) 
     { 
      var claims = new List<Claim>() 
      { 
       new Claim(CustomClaimTypes.UserId, session.UserId), 
       new Claim(CustomClaimTypes.BuId, session.BuId), 
       new Claim(CustomClaimTypes.SecurityLevel, session.SecurityLevel) 
      }; 

      var identity = new ClaimsIdentity(claims, "TNReadyEVP"); 
      var principal = new ClaimsPrincipal(identity); 

      await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, 
       new AuthenticationProperties { ExpiresUtc = DateTime.Now.AddMinutes(20), IsPersistent = true, AllowRefresh = false }); 

      var isAuthenticated = principal.Identity.IsAuthenticated; 
     } 
     else 
     { 
      return Forbid(); 
     } 

     return View("~/wwwroot/index.html"); 
    } 

答えて

1

まず私は、Core 2は、エラー報告で絶対に恐ろしいことを言いたいです。場合によっては、例外レポートがなくてもコードが失敗することがあります。それ以外のコア2は素晴らしいです。

これは答えです。私は、RedirectToPageとFileの使用を可能にするIActionResultを返すようにメソッドシグネチャを変更しました。

public async Task<IActionResult> OnGetAsync() 
    { 
     var request = HttpContext.Request; 
     var sessionId = request.Query.FirstOrDefault().Value; 
     var session = await _httpService.ValidateSession(sessionId); 

     if (!string.IsNullOrEmpty(sessionId)) 
     { 
      var claims = new List<Claim>() 
      { 
       new Claim(CustomClaimTypes.UserId, session.UserId), 
       new Claim(CustomClaimTypes.BuId, session.BuId), 
       new Claim(CustomClaimTypes.SecurityLevel, session.SecurityLevel) 
      }; 

      var identity = new ClaimsIdentity(claims, "TNReadyEVP"); 
      var principal = new ClaimsPrincipal(identity); 

      await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, 
       new AuthenticationProperties { ExpiresUtc = DateTime.Now.AddMinutes(60), IsPersistent = true, AllowRefresh = false }); 

      var isAuthenticated = principal.Identity.IsAuthenticated; 

      return File("index.html", "text/html"); 
     } 
     else 
     { 
      return RedirectToPage("Error"); 
     } 
    } 
+0

"コア2はエラーを報告する際に重大です。時には、例外が発生してもコードが失敗することがあります。" - それはバグであることを意味しますか、それは以前の.Netバージョンとは異なった動作をします。実際にすべてのエラーをキャプチャするにはもっと多くのことをする必要がありますか?これに関するより詳細な情報にリンクできますか?私は次のプロジェクトのために.Net Core 2を検討していましたが、これは私に一時停止を与えるかもしれません。 –

+1

私はそれがバグであることがわかります。ここに一番の例があります。 typenameを持つactionNameを持つデフォルトルートを設定します。コアは、エラーをコンソールウィンドウにプッシュするのではなく、単に空のページをレンダリングします。私が初心者として発見した他の奇妙な動作は、静的ファイルを提供するためにStartupクラスの適切なコードを設定すると、デフォルトのMVCルートは機能しないということでした。しかし、私はそれにこだわり、トンを学んだことがうれしいです! –

関連する問題