I am trying to create a login page. But it's saying no page found.
I have included this in Startup.cs
:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{Controller=LoginController}/{action=Login}");
});
The Login.cshtml
page is in views as Views/Login/Login.cshtml
and the controller lies under the Controllers folder with the name LoginController.cs
The code for LoginController.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AdminControl.Controllers
{
public class LoginController : Controller
{
// POST: LoginController
[HttpPost]
public IActionResult Login()
{
return View();
}
}
}
Login.cshtml
:
<form asp-action="Users/Index" class="form-group" id="login-form" method="post" runat="server" style="text-align: center;">
<input class="form-control" placeholder="ID" runat="server" style="text-align: center;"/>
<input class="form-control" placeholder="Password" runat="server" style="text-align: center;" type="password"/>
</form>
<button class="btn btn-info" form="login-form" value="Submit" type="submit">Submit</button>
What has gone wrong?
Fix the error in a pattern: "{Controller=LoginController}/{action=Login}");
pattern: "{Controller=Login}/{action=Login}");
But it's better to use a standard default pattern
pattern: "{controller=Home}/{action=Index}/{id?}");
and redirect from Index action to login controller after checking if user hasn't logined yet
And as @IsmailDiari noticed, you should have two actions - one to get login form, another to post login form. But you also need a Model:
public class LoginViewModel
{
public string Login {get; set;}
public string Password {get; set;}
}
After this change controller and views like this:
[HttpGet]
public IActionResult Login()
{
var viewModel=new LoginViewModel;
return View(viewModel);
}
// POST: When submitting the login credentials
[HttpPost]
public IActionResult Login(LoginViewModel viewModel)
{
}
LoginForm:
@model LoginViewModel
@using (Html.BeginForm())
{
<input class="form-control" asp-for = "Login" name="login" placeholder="Login" runat="server" style
="text-align: center;"/>
<input class="form-control" asp-for="Password" name="password" placeholder="Password" runat="server" style="text-align: center;" type="password"/>
<input type="submit" value="Login" class="btn btn-primary" />
}
Please see this link to know how to set a default route, you are doing that wrong. Also you need to provide Get action to return the view. your controller should be like the following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AdminControl.Controllers
{
public class LoginController : Controller
{
// Get: Return Login View
[HttpGet]
public IActionResult Login()
{
return View();
}
// POST: When submitting the login credentials
[HttpPost]
public IActionResult Login(FormCollection collection)
{
//1. validation Goes here
//2. redirection after validation,
//return View();
}
}
}
and your cshtml body should be something like this
<body>
@using (Html.BeginForm())
{
<input class="form-control" name="id" placeholder="ID" runat="server" style
="text-align: center;"/>
<input class="form-control" name="password" placeholder="Password" runat="server" style="text-align: center;" type="password"/>
<input type="submit" value="Login" class="btn btn-primary" />
}
</body>
you should set name attribute to your input so you can find them later in your FormCollection
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.