I've written below code for login view in ASP.NET Core MVC:
@using Microsoft.AspNetCore.Http
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
@{
ViewData["Title"] = "Login";
Layout = null;
}
<head>
</head>
<body>
@if (!string.IsNullOrEmpty(HttpContextAccessor.HttpContext.Session.GetString("User")))
{
//redirect to home page
}
<div class="main">
<p class="sign" align="center">Sign in</p>
<form class="form1" asp-action="Login" asp-controller="User" method="post" enctype="multipart/form-data">
<input class="un" type="text" align="center" placeholder="Username" name="username" id="username">
<input class="pass" type="password" align="center" placeholder="Password" name="Password" id="password">
<input type="submit" class="submit" align="center" value="Sign in" id="signIn">
</form>
</div>
<br />
</body>
If Session
contains data for key User
- then I don't want to show Login page
- instead I want to redirect the page to
Index
action of theHomeController
.
How can I achieve this?
There is no way but javascript to do that. This is because the view is rendering step of current request, you can not return redirect action. Also this is not good logic for MVC concept.
with javascript:
But I suggest you to achive it in your login controller. Before returning your action result, check if User exists in session, then redirect if required.