locked
pass data from action to action on web asp.net core 2.2 ? RRS feed

  • Question

  • User696604810 posted

    I have value on action and i need to get this value on another action on web api 

    are this possible ?

     public IActionResult PostUser([FromBody]Users user)
    {

    var user ="ahmed"

    }

     public IActionResult Postmenu([FromBody]Users user)
    {

    i need to use var user here 

    can i get value of ( var user) here

    }

    Wednesday, September 4, 2019 4:25 AM

All replies

  • User-1038772411 posted

    Hello, ahmedbarbary

    You can use "TempData" to send one action method value to another action method.

    Example :

    var user = "ahmed"
    
    TempData["xyz"] = user
    
    TempData.keep("xyz"); 
    
    Where you want to get means other action
    
    var data = TempData["xyz"];
    
    TempData.peek("xyz");

    This is only example code you can modify it like check null value and at all.

    Refer below link :

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.2

    Thanks.

    Wednesday, September 4, 2019 5:52 AM
  • User711641945 posted

    Hi ahmedbarbary,

    ahmedbarbary

    public IActionResult PostUser([FromBody]Users user)
    {

    var user ="ahmed"

    From your code,I am not clear what's the type of your user.You pass a Users class and name it user,but you give a value "ahmed" to user.It is impossible to give a string value to a Users class.

    Here is a workaround as follow:

    1.If you want to get a user value which is a Users class,you need to write a JSON storage extension to let you store complex objects:

     public static class SessionExtensions
        {
            public static void SetObjectAsJson(this ISession session, string key, object value)
            {
                session.SetString(key, JsonConvert.SerializeObject(value));
            }
    
            public static T GetObjectFromJson<T>(this ISession session, string key)
            {
                var value = session.GetString(key);
    
                return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
            }
        }

    2.Controller:

    [HttpPost]
    public string PostUser([FromBody]Users user)
    {
           HttpContext.Session.SetObjectAsJson("user", user);//store complex objects
    
           var item = "ahmed";
           HttpContext.Session.SetString("item", item);//store string value
           return "ss";
    }
    [HttpPost]
    public string Postmenu([FromBody]Users user)
    {
           var users = HttpContext.Session.GetObjectFromJson<Users>("user");
           var item = HttpContext.Session.GetString("item");
           return "ss";
    }

    3.Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        //...
         services.AddSession();
    
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //...
         app.UseSession();
         app.UseMvc();
    }

    Best Regards,

    Rena

    Wednesday, September 4, 2019 9:49 AM
  • User-474980206 posted

    websites are stateless. storing data between requests is your main design decision in a web site. see the docs for options:

       https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.2

    Wednesday, September 4, 2019 5:03 PM