简体   繁体   English

在 ASP.NET CORE 中重定向页面后保存字符串变量的最佳方法

[英]The best way to save the String Variable after being redirected Page in ASP.NET CORE

How can I save the form values as a string after the page is redirected?页面重定向后如何将表单值保存为字符串?

I use a static variable, if the number of users on the site increases for example 20000, will the site have problems?我使用一个 static 变量,如果站点上的用户数增加例如 20000,站点会有问题吗?

Which method is better?哪种方法更好?

  1. Using a static variable?使用 static 变量?

  2. session? session?

  3. Cookies? Cookies?

  4. ViewBag?查看包?

  5. View State?查看 State?

  6. sql server database? sql 服务器数据库?

Actually, There is no the best method, Only the most suitable method.其实,没有最好的方法,只有最合适的方法。 The methods you list above all have their own most suitable use cases.您上面列出的方法都有自己最合适的用例。

From your comment, I think you may wanna redirect to another page with a string parameter, So you can try to use:根据您的评论,我认为您可能想使用字符串参数重定向到另一个页面,因此您可以尝试使用:

return RedirectToAction("actionName","controllerName",new { Name="xxx"});

It will send a get request: acontrollerName/actionName?Name=xxx .它将发送一个获取请求: acontrollerName/actionName?Name=xxx

Or, if you wanna save that string value for a long time, You can also try to use Session or Database .或者,如果您想长时间保存该字符串值,您也可以尝试使用SessionDatabase But one thing you need to consider is that you need to set the session time out according to the actual situation of your project.但是您需要考虑的一件事是您需要根据项目的实际情况设置session超时。

==========================edited=========================== ===========================编辑======================= ====

I think you can use Session to achieve it.我认为您可以使用 Session 来实现它。

public class TestModel
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }


public async Task<IActionResult> OnPostSelectMenu(int selectedid)
        {
            TestModel selected = await _db.Table.FindAsync(selectedid);
             //set session
            HttpContext.Session.SetString("Key", JsonConvert.SerializeObject(selected));
            return RedirectToPage("MyPage");
        }

public async Task<IActionResult> OnGetAsync()
        {
            //......

            //get session
            Input = JsonConvert.DeserializeObject<TestModel>(HttpContext.Session.GetString("Key"));
            return Page();
        }

no i dont want to redirect to other page i want give string value after refresh current page How can I save the form values as a string after the page is refreshed?不,我不想重定向到其他页面我想在刷新当前页面后给出字符串值刷新页面后如何将表单值保存为字符串?

this is my model:这是我的 model:

public int ID { get; set; }
public string Name { get; set; }

static variable: static 变量:

public static int IDWordsaver = 0;

On Get for MyPage:在获取 MyPage 时:

public async Task<IActionResult> OnGetAsync()
        {
            await LoadAsync();
            return Page();
        }

private async Task LoadAsync()
        {
            if (IDWordsaver != 0)
            {
                var SetRecord = _db.Table.FirstOrDefault(u => u.ID == IDWordsaver);
                ID = IDWordsaver;
                Input = new InputModel
                {
                    Name = SetRecord.Name
                };
            }
        }

i use this button for select one record from data base:我将此按钮用于 select 数据库中的一条记录:

<button asp-page-handler="SelectMenu" name="selectedid" value=@item.ID class="btn btn-primary" formnovalidate>Select</button>


public async Task<IActionResult> OnPostSelectMenu(int selectedid)
        {
            var selected = await _db.Table.FindAsync(selectedid);
            ID = selected.ID;
            Input.Name = selected.Name;
            IDWordsaver = selectedid;
            return RedirectToPage("MyPage", new { IDWordsaver });
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心:session 过期,但用户未重定向到登录页面 - ASP.NET Core: session expires but user is not redirected to login page 字符串未在ASP.NET Core中翻译 - String is not being translated in ASP.NET Core asp.net Core/OnPostAsync:防止记录被创建两次的最佳方法是什么? - asp.net Core/OnPostAsync: what's the best way to prevent a record from being created twice? ASP.NET Core 5:登录页面和根页面使用常用 URL 的最佳方式 - ASP.NET Core 5: Best way to use common URL for login page and root page 当特定的数据库字段更改时,重新加载ASP.NET Core Razor 2.2网页的最佳方法 - Best way to reload ASP.NET Core Razor 2.2 web page when specific database field changes Asp.net核心Razor页面-页面加载后设置变量值 - Asp.net core Razor Page - Setting variable value after page loads ASP.NET Core - 减少业务服务依赖的最佳方法 - ASP.NET Core - Best way to reduce dependencies in business services 在Azure上存储ASP.NET Core应用程序密码的最佳方法 - Best way to store passwords for ASP.NET Core app on Azure 在 ASP.Net Core 中使用 HTTPClient 作为 DI 单例的最佳方法 - Best way to use HTTPClient in ASP.Net Core as a DI Singleton 在 asp.net core 中生成报告的最佳方式是什么? - What is the best way to produce the report in asp.net core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM