简体   繁体   English

在 ASP.NET MVC 中将参数从一个 controller 传输到另一个

[英]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.

相关问题 在ASP.NET MVC中将数据从一个控制器传递到另一个 - Passing data from one controller to another in asp.net mvc 如何将数据库信息传输到 asp.net mvc 中的另一个 controller - How to transfer database info to another controller in asp.net mvc 一个控制器的值到另一个控制器不在asp.net MVC? - one controller value to another controller not going in asp.net mvc? 如何将值从一个控制器传递到ASP.Net MVC3中的另一个控制器 - How to pass values from One controller to another Controller in ASP.Net MVC3 如何在ASP.NET MVC 4 Web应用程序中从一个控制器的视图重定向到另一个控制器的视图 - How to redirect to a View of another Controller from one Controller's View in ASP.NET MVC 4 Web Application 如何从 ASP.NET Core MVC 中的一个控制器调用或返回另一个控制器的视图 - How to call or return the view of another controller from one controller in ASP.NET Core MVC 将数据从一个控制器动作传递到ASP.Net MVC 5中的另一个控制器动作 - pass data from one controller action to another controller action in ASP.Net MVC 5 ASP.NET MVC 4从另一个控制器调用一个控制器方法 - ASP.NET MVC 4 call a controller method from another controller 从具有多个参数作为数据的请求处理程序中调用ASP.NET MVC控制器 - Invoking a ASP.NET MVC controller from a request handler with more than one parameter as data 如何将ASP.NET网页从一台PC传输到另一台PC? - How to transfer a ASP.NET webpage from one PC to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM