如何定义网址在浏览器中的显示方式?

时间:2019-01-15 10:30:43

标签: c# asp.net-mvc entity-framework authentication partial-views

基本上,我有一个简单的应用程序,下面附有类。

现在由于某些我无法理解的原因,默认页面将使用 http://localhost:[port]/Login/Index 显示,这很好。

这将显示索引页面,并以部分视图的形式生成登录表单。

由于某种原因,当我输入用户名和密码而不是登录登录方法时。它只是刷新页面,只是将URL更改为 http://localhost:[port]/Login/Login

一旦我插入了凭据,如果它们都正确无误,它将把我定向到正确的页面;否则,它将在屏幕上显示错误消息,并将URL再次更改为 http://localhost:[port]/Login/Login/Login

可能是什么问题?我感觉它与表单有关,即指向“登录/登录”的控制器名称动作名称,但是为什么整个路径都如此显示在网址中?

有什么建议吗?

RouteConfig.cs

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

Index.cshtml

@{
    ViewBag.Title = "Login Portal";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="horizontal-vertical-center">
    <ul class="nav nav-pills nav-justified">
        <li class="active"><a data-toggle="tab" href="#login">Login</a></li>
        <li><a data-toggle="tab" href="#register">Register</a></li>
    </ul>
    <div class="tab-content">
        <div id="login" class="tab-pane fade in active">
            @Html.Partial("Login")
        </div>
    </div>
</div>

Login.cshtml

@model BusinessService.DTO.Activity.UserDTO

@{
    Layout = null;
}

@using (Html.BeginForm("Login", "Login", FormMethod.Post, new { @class = "loginForm" }))
{
    @Html.AntiForgeryToken() // this is for prevent CSRF attack

    <div class="form-group">
        @Html.TextBoxFor(a => a.Email, new { @class = "form-control", @placeholder = "Email" })
        @Html.ValidationMessageFor(a => a.Email)
    </div>
    <div class="form-group">
        @Html.PasswordFor(a => a.Password, new { @class = "form-control", @placeholder = "Password" })
        @Html.ValidationMessageFor(a => a.Password)
    </div>

    if(ViewBag.ErrorMessage != null)
    {
        <div class="alert alert-danger" role="alert">
            @ViewBag.ErrorMessage
        </div>
    }

    <button type="submit" class="btn btn-primary">Login</button>
}

显然是主控制器LoginController.cs

using BusinessService.DTO.Activity;
using BusinessService.Services.Interfaces;
using System.Web.Mvc;
using System.Web.Security;

namespace LoginPortal.Controllers
{
    public class LoginController : Controller
    {
        private ILoginService _loginService;

        public LoginController(ILoginService loginService)
        {
            this._loginService = loginService;
        }

        // GET: Login
        [AllowAnonymous]
        public ActionResult Index(string ReturnUrl)
        {
            if (ReturnUrl != "")
                TempData["returnUrl"] = ReturnUrl;

            return View();
        }

        public ActionResult Login()
        {
            if (!Request.IsAjaxRequest())
                return RedirectToAction("Index");

            return PartialView();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(UserDTO model)
        {
            if (ModelState.IsValid)
            {
                var userResult = _loginService.VerifyUser(model);

                if (userResult != null)
                {
                    if (!userResult.EmailConfirmed)
                        return RedirectToAction("ConfirmEmail");
                    else
                    {
                        FormsAuthentication.SetAuthCookie(userResult.Email, false);
                        Session["User"] = new { userResult.Id, userResult.FullName, userResult.Email };

                        return Redirect("http://localhost:49770/Admin");
                    }
                }
            }

            ViewBag.ErrorMessage = "Incorrect Username or Password";
            return View("Index");
        }

        private ActionResult Unauthorized()
        {
            return View();
        }

        private ActionResult ConfirmEmail()
        {
            return View();
        }
    }
}

下面附有图片:

enter image description here

enter image description here

更新: 请在下面找到网络日志。如您所见,为了访问Login(UserDTO dto)方法,进行了两次调用。

enter image description here

0 个答案:

没有答案