【问题标题】:How to avoid long and complex URL when using RedirectToPage?使用 RedirectToPage 时如何避免长而复杂的 URL?
【发布时间】:2021-03-30 17:32:21
【问题描述】:

我正在使用控制器将数据返回到 Razor 页面。我正在返回员工信息:

public IActionResult View(int id)
    {
        var employee = _context.Employees.Where(i => i.Id == id).Select(i => new {
            i.FirstName,
            i.LastName,
            i.PhoneNumber,
            i.Email
        });
        string employee = JsonConvert.SerializeObject(employee);
        return RedirectToPage("/Employees/View", new { empId = id, emp = employee });
    }

我使用 Razor 页面模型的 OnGet() 来获取名字等,操作它们,然后在页面上相应地显示。这真的很好,除了所有这些信息都在 URL 中可见。这是页面正在做的一小部分,我需要使用页面而不是视图。您能否告诉我如何将员工信息传递到页面而不在 URL 中显示?

【问题讨论】:

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


    【解决方案1】:

    我认为你可以使用TempData:

    public IActionResult View(int id)
    {
        var employee = _context.Employees.Where(i => i.Id == id).Select(i => new {
            i.FirstName,
            i.LastName,
            i.PhoneNumber,
            i.Email
        });
        string employee = JsonConvert.SerializeObject(employee);
        
        //add the following code....
        TempData["empId "] = id;
        TempData["employee"] = employee;
    
        return RedirectToPage("/Employees/View");
    }
    

    在您的/Employees/View 方法中:

    public IActionResult View()
    {
        var emp = TempData["employee"] as string;
        var empId = TempData["empId "];
        return Page();
    }
    

    参考:

    https://stackoverflow.com/a/63461336/11398810

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-24
      • 2010-12-15
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      相关资源
      最近更新 更多