I have created a login action method to log a user in, while I have already scaffolded the model and view, I am now trying to add that route to a specific page, but I get error 404 as it can't find the route.
Below is the UsersController.cs :
// GET: Users
public ActionResult Index()
{
return View(db.Users.ToList());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(User objUser)
{
if (ModelState.IsValid)
{
using (DE_StoreContext db = new DE_StoreContext())
{
var obj = db.Users.Where(a => a.username.Equals(objUser.username) && a.Password.Equals(objUser.Password)).FirstOrDefault();
if (obj != null)
{
Session["UserID"] = obj.id.ToString();
Session["UserName"] = obj.username.ToString();
return RedirectToAction("UserDashBoard");
}
}
}
return View(objUser);
}
This is the webpage Login.cshtml:
@model DE_Store.Models.User
@{
ViewBag.Title = "Login";
}
@using (Html.BeginForm("Login", "Users", FormMethod.Post))
{
<fieldset>
<legend>Mvc Simple Login Application Demo</legend>
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@if (@ViewBag.Message != null)
{
<div style="border: 1px solid red">
@ViewBag.Message
</div>
}
<table>
<tr>
<td>@Html.LabelFor(a => a.username)</td>
<td>@Html.TextBoxFor(a => a.username)</td>
<td>@Html.ValidationMessageFor(a => a.username)</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Password)
</td>
<td>
@Html.PasswordFor(a => a.Password)
</td>
<td>
@Html.ValidationMessageFor(a => a.Password)
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Login" />
</td>
<td></td>
</tr>
</table>
</fieldset>
}
And this is the route.config.cs
file that I am using for routing:
routes.MapRoute(
name: "Login",
url: "{controller}/{action}/{id}",
new { controller = "Users", action = "Login", id = UrlParameter.Optional }
);
My personal idea is either that I have defined it wrongly in the Login
page or the custom route is wrong, I have been trying to use a tutorial to add the Login
action method. I have tried accessing from another part of the web app by adding a link, but it can't find the /Users/Login route
CodePudding user response:
you have to specify controller? return RedirectToAction("UserDashBoard", "Home");
CodePudding user response:
@Las Sincas. This is how your controller code should look like. Note that you need the Login Action as Jackdaw has also indicated. That is the action that your route is pointing to.
using DE_Store.Models;
using System.Linq;
using System.Web.Mvc;
namespace DE_Store.Controllers
{
public class UsersController : Controller
{
// GET: Users
public ActionResult Index()
{
return View(db.Users.ToList());
}
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(User objUser)
{
if (ModelState.IsValid)
{
using (DE_StoreContext db = new DE_StoreContext())
{
var obj = db.Users.Where(a => a.username.Equals(objUser.username) && a.Password.Equals(objUser.Password)).FirstOrDefault();
if (obj != null)
{
Session["UserID"] = obj.id.ToString();
Session["UserName"] = obj.username.ToString();
return RedirectToAction("UserDashBoard");
}
}
}
return View(objUser);
}
}
}
Otherwise if you only have an Index action, keep the default route as explained by Wiktor, otherwise change your mapping to this:
routes.MapRoute(
name: "Login",
url: "{controller}/{action}/{id}",
new { controller = "Users", action = "Index", id = UrlParameter.Optional }
);