My view does not starta

Asked

Viewed 4,278 times

0

This is the Error that gives:

Erro de Servidor no Aplicativo '/'.

Não é possível encontrar o recurso.

Descrição: HTTP 404. O recurso que você está procurando (ou uma de suas dependências) não pôde ser removido, seu nome foi alterado ou está temporariamente indisponível. Examine o URL e certifique-se de que está digitado corretamente. 

URL solicitada: /Login/Login

Informações sobre a Versão: Microsoft .NET Framework Versão:4.0.30319; Versão do ASP.NET:4.0.30319.34009

This is my CSHTML

<!DOCTYPE html>

<html> <head>
    <meta name="viewport" content="width=device-width" />
    <title>Login</title> </head> <body>
    <h2>
        Login do sistema administrativo
    </h2>
    <% Html.EnableClientValidation(); %>
    <%= Html.ValidationSummary("Não foi possível realizar o login, corrija os erros abaixo e tente novamente.") %>
    <div id="loginForm">
        <% using (Html.BeginForm(FormMethod.Post))
        { %>
        <%= Html.AntiForgeryToken() %>
        <b>Login:</b>
        <%= Html.TextBox("LOGIN", "", new { maxlength = "10" })%><br />
        <b>Senha</b>
        <%= Html.Password("SENHA", "", new { maxlength = "10" })%>&nbsp;<input type="submit" value="Entrar" />
        <%} %>
    </div> </body> </html>

This is my controller

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(string login, string senha)
        {
             bool userValidate = false;

            if (ModelState.IsValid)
            {
                if (login != "" && senha != "")
                {
                    userValidate = false;
                    ModelState.AddModelError("", "Dados de login inválidos");
                }
                else
                {
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "Administrador", DateTime.Now, DateTime.Now.AddMinutes(30), false, "admin", FormsAuthentication.FormsCookiePath);

                    string hashCookies = FormsAuthentication.Encrypt(ticket);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);

                    Response.Cookies.Add(cookie);

                    userValidate = true;
                }


            }
            //if (!userValidate)
            //    return View();
            //else
            //    return RedirectToAction("Index", "Main");

            return View();
        }

This is my route

routes.MapRoute(
                name: "Login",
                url: "Login",
                defaults: new { controller = "Login", action = "Login", name = "" }
            );

Apparently everything is ok, so I ask: What is missing to climb this View? When I remove Httppost or create another Action as suggested by Taferel Chicotti, this is the output in the View:

Login do sistema administrativo

<% Html.EnableClientValidation(); %> <%= Html.ValidationSummary("Não foi possível realizar o login, corrija os erros abaixo e tente novamente.") %>
<% using (Html.BeginForm(FormMethod.Post)) { %> <%= Html.AntiForgeryToken() %> Login: <%= Html.TextBox("LOGIN", "", new { maxlength = "10" })%>
Senha <%= Html.Password("SENHA", "", new { maxlength = "10" })%>  <%} %>
  • The action attribute is not [Httpget]?

  • Why is your url like this? 'Requested URL: /Login/Login' with this duplicated "Login"? Wouldn’t that be the problem?

  • Removing the attribute also does not work.

  • The Controller is called Login and the View is also Login, so the url would be> .../Login/Login

  • 2

    the way q is... you will not be able to access pq is asking for the existence of an Httppost and a string as login and a string as password. To fix, without changing this action, you create a second action like this: [Httpget] public Actionresult Login () { Return View(); }

1 answer

2

The comment is clear: two are needed Actions to function, being one for the verb POST and another for the verb GET:

public class LoginController : Controller 
{
    public ActionResult Login() 
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(string login, string senha)
    {
         bool userValidate = false;

        if (ModelState.IsValid)
        {
            if (login != "" && senha != "")
            {
                userValidate = false;
                ModelState.AddModelError("", "Dados de login inválidos");
            }
            else
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "Administrador", DateTime.Now, DateTime.Now.AddMinutes(30), false, "admin", FormsAuthentication.FormsCookiePath);

                string hashCookies = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);

                Response.Cookies.Add(cookie);

                userValidate = true;
            }


        }
        //if (!userValidate)
        //    return View();
        //else
        //    return RedirectToAction("Index", "Main");

        return View();
    }
}

To Action marked with [HttpPost] is only accessible by sending a form. The other simply displays the View empty.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.