I've tried to do something from another post, didn't work. How do I combine those two codes? HTML CODE:
@using Microsoft.AspNetCore.Identity
@using SvivaTeamVersion3.Areas.Identity.Data
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
<p><button onclick="" ><b>ADD NEW REPORT »</b></button></p>
Code that I want to add to the onclick event:
if (SignInManager.IsSignedIn(User))
{
<script>
window.location.href = "http://stackoverflow.com";
</script>
} else {
<script>
window.location.href = "http://stackoverflow.com";
</script>
}
CodePudding user response:
As I understood that you need a conditional access to you page based on User authentication state, so this should work:
@using Microsoft.AspNetCore.Identity
@using SvivaTeamVersion3.Areas.Identity.Data
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
<p><button onclick="" ><b>ADD NEW REPORT »</b></button></p>
@if (SignInManager.IsSignedIn(User))
{
<script>
window.location.href = "http://stackoverflow.com";
</script>
} else {
<script>
window.location.href = "http://stackoverflow.com";
</script>
}
A better approach would be to use OnGet() method in your razor page model:
public IActionResult OnGet()
{
if(SignInManager.IsSignedIn(User))
Redirect("http://stackoverflow.com");
else
Redirect("http://stackoverflow.com");
}
CodePudding user response:
If you want to use c#code in js,you can try to use:
<script>
var url;
$(function(){
[email protected](User)?"url1":"url2";
window.location.href =url;
})
</script>