C# – How pass objects from one page to another on ASP.Net Core with razor pages

asp.net-corecrazor

I'm developing a web application using Asp.net core 2.0 with razor pages. I'm creating an object with some data and want to send that object to another page.

Actually I'm doing this:

var customObject = new{
   //some values
};
return RedirectToPage("NewPage", customObject);

I see that the url has the values of the object I'm sending, but I can't find how to take that values (in the NewPage instance).

Can anybody knows how to share objects between razor pages? Is this the correct way to achieve it? or Is there another better way?

Thanks in advance

Best Answer

You can pass parameters to specified handlers in the page model class, like this:

return RedirectToPage("Orders", "SingleOrder", new {orderId = order.Id});

Where the page model class method has this signature:

public void OnGetSingleOrder(int orderId)

If you are passing an object, you can pass the object directly, but in my experience any child objects are not populated.