C# 创建 API 和 ASP.NET MVC Web 应用程序 - 需要帮助才能收到输入错误参数的警告

问题描述 投票:0回答:1

我正在使用 SQL Server 创建 ASP.NET Web API 和 MVC Web 应用程序。它运行良好,授权成功,但是当我测试登录屏幕时,如果我输入错误的内容(例如错误的密码),它不会返回警告。

这是我的

AccountController
的登录代码

[HttpPost]
public async Task<ActionResult> Login(LoginViewModel model)
{
    if (ModelState.IsValid)
    {
        var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
        var response = await _httpClient.PostAsync("https://localhost:7180/api/auth/login", content);

        if (response.IsSuccessStatusCode)
        {
            var tokenResponse = await response.Content.ReadAsStringAsync();
            var token = JsonConvert.DeserializeObject<TokenResponse>(tokenResponse);

            if (HttpContext.Session != null)
            {
                HttpContext.Session.SetString("JWToken", token.Token);
            }
            else
            {
                ModelState.AddModelError("", "Session has not been configured.");
                return View(model);
            }

            TempData["Token"] = token.Token;
        }
        else
        {
            ModelState.AddModelError("", "Kullanıcı adı veya şifre 
               hatalı.");
            ViewData["ErrorMessage"] = "Kullanıcı adı veya şifre 
               hatalı.";
        }
        
    }

    return View(model);
}

还有我的

login.cshtml
观点:

@page
@model WebApplication2.Models.LoginViewModel
@{
    ViewBag.Title = "Login";
}
<h2>Login</h2>

@using (Html.BeginForm("Login", "Account", FormMethod.Post))
{
    <div class="form-group">
        @Html.LabelFor(m => m.ADI)
        @Html.TextBoxFor(m => m.ADI, new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.SOYADI)
        @Html.TextBoxFor(m => m.SOYADI, new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.KULLANICI_ADI)
        @Html.TextBoxFor(m => m.KULLANICI_ADI, new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.SIFRE)
        @Html.PasswordFor(m => m.SIFRE, new { @class = "form-control" })
    </div>
    <button type="submit" class="btn btn-primary">Login</button>

    @if (ViewData != null && ViewData.ModelState.ContainsKey(""))
    {
        <div class="alert alert-danger mt-3" role="alert">
            @Html.ValidationSummary(false, "", new { @class = "text-danger" })
        </div>
    }
        
}
@if (TempData["Token"] != null)
{
    <div class="alert alert-success mt-3" role="alert">
        Bearer Token:
        <button class="btn btn-info" id="toggleToken">Toggle Token</button>
        <div id="tokenContent" style="display: none;">
            <pre>@TempData["Token"]</pre>
        </div>
    </div>
    <div><p><a href="GetUsers">getallusers</a></p></div>
}

@section Scripts {
    <script>
        $(document).ready(function () {
            $('#toggleToken').click(function () {
                $('#tokenContent').slideToggle();
            });
        });
        
    </script>
}

我希望在输入错误的名字、姓氏、用户名或密码或我想看到的所有内容时看到有关它们的警告...

非常感谢

c# asp.net-mvc asp.net-web-api
1个回答
0
投票

在表单字段顶部添加验证摘要方法,以在顶部显示所有错误,或在每个表单字段下添加验证消息方法,以显示每个表单字段下的默认错误。如果您想在访问服务器之前验证输入错误,您还可以使用基于 Jquery 的客户端表单验证器。这里是如何在 HTML 端显示错误,即

在您的 CSHTML 中,在顶部添加验证摘要:

... 

@using (Html.BeginForm("Login", "Account", FormMethod.Post))
{
   <hr />  
   @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
   <hr /> 

    <div class="form-group">
        @Html.LabelFor(m => m.ADI)
        @Html.TextBoxFor(m => m.ADI, new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.SOYADI)
        @Html.TextBoxFor(m => m.SOYADI, new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.KULLANICI_ADI)
        @Html.TextBoxFor(m => m.KULLANICI_ADI, new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.SIFRE)
        @Html.PasswordFor(m => m.SIFRE, new { @class = "form-control" })
    </div>

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

...

在您的 CSHTML 中为每个字段添加验证消息:

... 

@using (Html.BeginForm("Login", "Account", FormMethod.Post))
{
    <div class="form-group">
        @Html.LabelFor(m => m.ADI)
        @Html.TextBoxFor(m => m.ADI, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.ADI, "", new { @class = "text-danger" }) 
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.SOYADI)
        @Html.TextBoxFor(m => m.SOYADI, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.SOYADI, "", new { @class = "text-danger" })
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.KULLANICI_ADI)
        @Html.TextBoxFor(m => m.KULLANICI_ADI, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.KULLANICI_ADI, "", new { @class = "text-danger" })
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.SIFRE)
        @Html.PasswordFor(m => m.SIFRE, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.SIFRE, "", new { @class = "text-danger" })
    </div>

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

...
© www.soinside.com 2019 - 2024. All rights reserved.