【问题标题】:How can i hide ActionLink Parameter or queryString from URL?如何从 URL 中隐藏 ActionLink 参数或 queryString?
【发布时间】:2020-09-25 08:44:45
【问题描述】:

如何隐藏 URL 中的查询参数?

我有三页,我用Context.Request.QueryPage 1.cshtml传输数据>>Page 2.cshtml>>Page 3.cshtml都很好。但我需要你的帮助来隐藏 Url 中的数据

在我看来,我有以下代码

var ProductID= Context.Request.Query["ProductID"];
var ProductName= Context.Request.Query["ProductName"];

和操作链接到导航

@Html.ActionLink("Create", "Create", "Product", 
                   new { Area = "Products", 
                       id = ProductID,                           
                       ProductName = ProductName                        
                   }, 
                   new { @class = "btn btn-primary" })

【问题讨论】:

  • 有很多方法可以做到这一点。请问你为什么要隐藏查询字符串?

标签: asp.net asp.net-mvc asp.net-core razor asp.net-core-mvc


【解决方案1】:

正如评论所说,有很多方法可以实现,关键在于您的需求。

一种方法,如果参数是从page1code behind in the controller传过来的,那么我建议你可以直接把需要传的参数归档到TempData,这样你不需要使用actionlink来传递参数。

  public IActionResult Page1()
    {
        TempData["Area"] = "Products";
        TempData["id"] =1;
        TempData["ProductName"] = "name";
        return View();
    }

    public IActionResult Create()
    {
        string Area = TempData["Area"] as string;
        int id = (int)TempData["id"];
        string ProductName = TempData["ProductName"] as string;
        return View();
    }

第二种方法,如果允许,可以将这些参数放在form中绑定各个值,如果不想显示这些值,可以设置为@987654327 @类型。

<form asp-action="Create" asp-controller="Product">
    <input id="Text1" type="hidden" name="Area" value="Products" />
    <input id="Text1" type="hidden" name="id" value="1" />
    <input id="Text1" type="hidden" name="ProductName" value="name" />

    <input id="Button1" type="submit" value="Create"  class="btn btn-primary"/>
</form>

创建动作:

public IActionResult Create(string Area, int id, string ProductName)
    { 
        return View();
    }

第三种方法,如果必须将参数从链接传递到另一个视图,则使用ajax结合TempData,请参考以下案例:

  public IActionResult Create(string Area, int id, string ProductName)
        {
            if (Area != null && id != 0 && ProductName != null)
            {
                TempData["Area"] = Area;
                TempData["id"] = id;
                TempData["ProductName"] = ProductName;
            }
            else
            {
                Area = TempData["Area"] as string;
                id = (int)TempData["id"];
                ProductName = TempData["ProductName"] as string;
            } 
            return View();
        }

查看:

@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Scripts{
    <script>
        $(function () {
            $(".mylink").click(function () {
                event.preventDefault();
                var href = $(this).attr("href").split('?')[0];
                var query = $(this).attr("href").split('?')[1];
                var arr = query.split("&");
                result = {};
                for (i = 0; i < arr.length; i++) {
                    k = arr[i].split('=');
                    result[k[0]] = (k[1] || '');
                };
                $.ajax({
                    method: "Get",
                    url: href,
                    data: query,
                    success: function () {
                        window.location.href = href;
                    }
                });

            });

        })
    </script>
}
<h1>Index</h1>
    @Html.ActionLink("Create", "Create", "Product",
                       new
                       {
                           Area = "Products",
                           id = 1,
                           ProductName = "name"
                       },
                       new { @class = "btn btn-primary mylink" })

第二种方法的更新:

create action 从其他视图接收到这些值后,您可以使用 modelViewData 在 create view 中显示数据:

型号

public class Test
{ 
   public int id{ get; set; }
   public string Area { get; set; }
   public string ProductName { get; set; }  
}

创建动作:

  public IActionResult Create(string Area, int id, string ProductName)
    { 
        Test myTest = new Test()
        {
            id = id,
            Area = Area,
            ProductName = ProductName
        };
        return View(myTest);
    }

创建视图:(在视图中显示值)

@model WebApplication_core_mvc.Models.Test
@{
    ViewData["Title"] = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Create</h1>
<table class="table table-bordered">
    <tr>
          <th>id</th>
          <th>Area</th>
          <th>ProductName</th>
    </tr>
    <tr>
        <td>@Model.id</td>
        <td>@Model.Area</td>
        <td>@Model.ProductName</td>
    </tr>

查看数据

创建动作:

public IActionResult Create(string Area, int id, string ProductName)
        { 
            ViewData["id"] = id;
            ViewData["Area"] = Area;
            ViewData["ProductName"] = ProductName; 
            return View();
        }

创建视图:(在视图中显示值)

@{
        ViewData["Title"] = "Create";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h1>Create</h1>


    <table class="table table-bordered">
        <tr>
           <th>id</th>
          <th>Area</th>
          <th>ProductName</th>
        </tr>
        <tr>
            <td>@ViewData["id"]</td>
            <td>@ViewData["Area"]</td>
            <td>@ViewData["ProductName"]</td>
        </tr>
    </table>

这是测试结果:

【讨论】:

  • 谢谢你,我刚看到你的回放,我会试试,让你知道。看起来很棒!谢谢你的帮助!
  • 再次感谢您,在第二种方法中,如何在视图中显示传递的值?
  • @Rob,你可以使用ViewData或者create a model在视图中显示这些传递的值,我已经更新了我的帖子,你可以参考一下。
  • 我测试了它,并且工作我没有尝试它,我知道它肯定会起作用但是Yongqing Yu你很有帮助,你的答案很清楚..你太棒了,非常感谢
  • 如果您对此有任何想法stackoverflow.com/questions/62355063/…
猜你喜欢
  • 2016-12-02
  • 1970-01-01
  • 2011-06-03
  • 2017-03-26
  • 1970-01-01
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多