[英]Transfer parameter from one controller to another in ASP.NET MVC
I need to transfer one parameter (string) that I create in one controller to another controller.我需要将我在一个 controller 中创建的一个参数(字符串)传输到另一个 controller。 Till today I used global variable and it worked fine, but now a few users use the application, so I can't verify that the parameter was not rewrite by other users.
直到今天我使用全局变量并且它工作正常,但现在有几个用户使用该应用程序,所以我无法验证该参数没有被其他用户重写。 The parameter is unique.
该参数是唯一的。 I tried to use HttpContext.Session, but cannot be used in another controller (just returns null).
我尝试使用 HttpContext.Session,但不能在另一个 controller 中使用(只返回 null)。
Do you have any idea how to do it?你知道怎么做吗?
And also, is it possible to use one instance of HttpContext.Session in different controllers?而且,是否可以在不同的控制器中使用 HttpContext.Session 的一个实例?
Thank you!谢谢!
Create global static
variable inside controller
class.在
controller
class 内创建global static
变量。
public class HomeController
{
public static string object_name { get; set; }
}
Now in another controller use DependencyResolver
to initialize HomeController
现在在另一个 controller 使用
DependencyResolver
来初始化HomeController
public class MyController
{
private readonly HomeController homeController = DependencyResolver.Current.GetService<HomeController>();
homeController.object_name = "" //Set value
var a = homeController.object_name; //Get value
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.