简体   繁体   English

自定义路由在 ASP.NET MVC 应用程序 C# 中给出错误 404

[英]Custom route gives error 404 in ASP.NET MVC application C#

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.我创建了一个登录操作方法来登录用户,虽然我已经搭建了模型和视图的脚手架,但我现在正在尝试将该路由添加到特定页面,但是我收到错误 404,因为它找不到路由。

Below is the UsersController.cs :下面是 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:这是网页 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:这是我用于路由的route.config.cs文件:

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.我个人的想法是要么是我在Login页面定义错了要么是自定义路由错误,我一直在尝试使用教程添加Login动作方法。 I have tried accessing from another part of the web app by adding a link, but it can't find the /Users/Login route我尝试通过添加链接从 Web 应用程序的另一部分访问,但找不到 /Users/Login 路由

you have to specify controller?你必须指定控制器? return RedirectToAction("UserDashBoard", "Home"); return RedirectToAction("UserDashBoard", "Home");

@Las Sincas. @拉斯辛卡斯。 This is how your controller code should look like.这就是您的控制器代码的样子。 Note that you need the Login Action as Jackdaw has also indicated.请注意,您还需要 Jackdaw 指出的登录操作。 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:否则,如果您只有一个 Index 操作,请按照 Wiktor 的说明保留默认路由,否则将您的映射更改为:

        routes.MapRoute(
            name: "Login",
            url: "{controller}/{action}/{id}",
                new { controller = "Users", action = "Index", id = UrlParameter.Optional }
            );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM