【问题标题】:Pass RouteValues from Action to HTML BeginForm将 RouteValues 从 Action 传递到 HTML BeginForm
【发布时间】:2020-02-26 12:52:53
【问题描述】:

我试图将单个路由值从我的 Action 方法传递到 HTML。我正在尝试将保存在我的 url 中的单个令牌值传递给另一个 Action 方法。

我的操作方法

public async Task<IActionResult> LogIn(User user)
        {
            try
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(
                    Encoding.UTF8.GetBytes(user.Username + ":" + user.Password)));

                HttpResponseMessage clientTask = await client.GetAsync("https://localhost:44324/api/Auth/LogIn");

                if (clientTask.IsSuccessStatusCode)
                {
                    string txtBlock = await clientTask.Content.ReadAsStringAsync();

                    var tokenObject = JsonConvert.DeserializeObject<SessionAPI>(txtBlock);

                    client.DefaultRequestHeaders.Authorization = null;

                    Request.RouteValues["token"] = tokenObject.Token;

                    return RedirectToAction("Index", "Home", Request.RouteValues);
                }
                else
                    return View("LogInIndex", user);
            }
            catch(Exception e)
            {
                throw new Exception("An Error has occured" + e);
            }
        }

我的 HTML

@{
    ViewData["Title"] = "Home Page";
}

    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
        <p></p>
        <p></p>
        <div>
            @using (Html.BeginForm("LogOut", "Auth", FormMethod.Get))
            {
                <input type="submit" value="Log Out" />
            }

        </div>
        <div>
            @using (Html.BeginForm("ListAll", "User", FormMethod.Get)) // <-- Trying to pass a route value for this form
            {
                <input type="submit" value="List All" />
            }

        </div>
        <div>
            @using (Html.BeginForm("CreateIndex", "User", FormMethod.Post))
            {
                <input type="submit" value="Create a User" />
            }

        </div>
    </div>

基本思想是拥有一个存储在 URL 中的令牌,并在传递新请求时与它一起发送并分配给标头以进行身份​​验证

【问题讨论】:

    标签: c# asp.net .net asp.net-core .net-core


    【解决方案1】:

    你可以使用ViewBag,它可以传递一个数据来查看,在你的代码中做一些修改如下:

    1.登录动作,传递RedirectToAction中的路由值,如:

     return RedirectToAction("Index", "Home", new { token=tokenObject.Token});
    

    2.索引操作,将token保存在ViewBag中

     ViewBag.Token = Request.Query["token"];
    

    3.索引视图,千万不要使用GET发送敏感数据! (将在 URL 中可见),您可以使用隐藏的输入来发送数据

    @using (Html.BeginForm("ListAll", "User", FormMethod.Get)) 
    {
        <input hidden name="token" value="@ViewBag.Token"/>
        <input type="submit" value="List All" />
    }
    

    4.ListAll action,使用参数获取数据

        public IActionResult ListAll(string token)
        { ... }
    

    参考: ASP.NET MVC Core Tag Helper Issue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      相关资源
      最近更新 更多