简体   繁体   English

如何从控制器MVC.NET重定向到.aspx

[英]How do I redirect to a .aspx from a Controller MVC.NET

I have a Login.aspx file in my folder Shared. 我的共享文件夹中有一个Login.aspx文件。 When I click my logut button, it takes me to SessionController->LogOut action. 当我单击logut按钮时,它将带我到SessionController-> LogOut操作。 I have to problems. 我有问题。

  1. My FormsAuthentication is not beign signed out. 我的FormsAuthentication尚未退出。

  2. Gets an error when redirecting to Login.aspx: 重定向到Login.aspx时获取错误:

System.InvalidOperationException: The view at '~/Views/Shared/Login.aspx' must derive from ViewPage, ViewPage, ViewUserControl, or ViewUserControl. System.InvalidOperationException:“〜/ Views / Shared / Login.aspx”处的视图必须派生自ViewPage,ViewPage,ViewUserControl或ViewUserControl。

I got this code: 我得到以下代码:

My Controller: 我的控制器:

public class SessionController : Controller
{
    public ActionResult LogOut()
    {
        if (Request.Cookies["Usuario"] != null)
        {
            FormsAuthentication.SignOut();
            var c = new HttpCookie("Usuario");
            c.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(c);
        }
        return View("Login");
    }
}

My calling from HTML to de Conttroller: 我从HTML致电de Conttroller:

<ul class="dropdown-menu">
    <li><a id="logout" href="~/Session/LogOut">Log out</a></li>
</ul>

And Login.aspx: 和Login.aspx:

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Security " %>
<%@ Import Namespace="System.IO" %>

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Forms Authentication</title>
    <link href="https://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://getbootstrap.com/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
    <link href="https://getbootstrap.com/examples/signin/signin.css" rel="stylesheet">
    <script src="https://getbootstrap.com/assets/js/ie-emulation-modes-warning.js"></script>

    <script runat="server">
        private void Logon_Click(Object sender, EventArgs e)
        {
            string cmd = "User='" + User.Value + "'";
            DataSet ds = new DataSet();
            FileStream fs = new FileStream(Server.MapPath("Users.xml"), FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(fs);
            ds.ReadXml(reader);
            fs.Close();
            DataTable users = ds.Tables[0];
            DataRow[] matches = users.Select(cmd);
            if (matches != null && matches.Length > 0)
            {
                DataRow row = matches[0];
                string hashedpwd = FormsAuthentication.HashPasswordForStoringInConfigFile(Password.Value, "SHA1");
                string pass = (String)row["Password"];
                if (0 != String.Compare(pass, hashedpwd, false))
                    Msg.Text = "<div class='alert alert-danger'>Usuario y contraseña no coinciden.</div>";
                else
                {
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                        User.Value,
                        DateTime.Now,
                        DateTime.Now.AddDays(5),
                        Persist.Checked,
                        "abc",
                        FormsAuthentication.FormsCookiePath);
                    string encTicket = FormsAuthentication.Encrypt(ticket);
                    Response.Cookies.Add(new HttpCookie("Usuario", encTicket));
                    Response.Redirect("~/Factura");


                }
                /*FormsAuthentication.SetAuthCookie(User.Value, Persist.Checked);
                Response.Redirect("~/Factura");*/
            }
            else
            {
                Msg.Text = "<div class='alert alert-danger'>Usuario y contraseña no coinciden.</div>";
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <form runat="server" class="form-signin">
            <h2 class="form-signin-heading">Login</h2>
            <label for="User" class="sr-only">Email address</label>
            <input id="User" class="form-control" type="text" placeholder="Nombre de usuario" title="Solo numeros y letras" pattern="^[A-Za-z]*$" required autofocus runat="server" />
            <label for="Password" class="sr-only">Contraseña</label>
            <input id="Password" class="form-control" type="password" placeholder="Contraseña" required runat="server" />
            <asp:Literal ID="Msg" runat="server" />
            <div class="checkbox">
                <label>
                    <asp:CheckBox ID="Persist" runat="server"
                        AutoPostBack="true" />
                    Recordar contraseña
                </label>
            </div>
            <input type="submit" class="btn btn-lg btn-primary btn-block" onserverclick="Logon_Click" value="Login"
                runat="server" />
        </form>
    </div>
    <script src="https://getbootstrap.com/assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>

As the exception says, MVC views have to inherit specific MVC infrastructure classes in order to be recognized as MVC views. 如异常所示,MVC视图必须继承特定的MVC基础结构类,才能被识别为MVC视图。

In case of the ASPX view engine you need to specify this in an explicit way, for example 对于ASPX视图引擎,您需要以显式方式指定此名称,例如

<%@ Page Language="C#" 
    Inherits="System.Web.Mvc.ViewPage<YourModelClassName>" %>

or 要么

    Inherits="System.Web.Mvc.ViewPage"

if your view doesn't expect any model. 如果您的视图不需要任何模型。

Hope this helps 希望这可以帮助

return Redirect("url");

or 要么

return RedirectToRoute("routename"); // Specify route in the Route Config file.

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

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