How to pass data between a razor page (*.cshtml) and Razor component (*.razor)

Balu Raju 81 Reputation points
2023-11-27T00:15:46.0166667+00:00

I have a situation where I have a razor page -sign.cshtml that handles OAuth by using PageModel's OnGetAsync and OnGetCallbackAsync

Question #1 How can I do this alternatively just using Razor component (*.razor) instead of ".cshtml"?

I also inject a viewmodel and a shared service ("sharedservice") that holds user data.

In OnCallBackAsync, upon successful Oauth authetication I save user data in an object "OAuthUser" object via shared service that I injected.

in the same call back method I use LocalRedirect to a razor component (signup.razor)

In the singup.razor I inject the shared service ("sharedservice") but instance I get is new instance instead of the one used by sign.cshtml, so I lost the user's data saved upon authentication.

I came to know that using LocalRedirect causes a new request and hence it creates a new instance, is this correct?

QUestion #3

How can I sustain the user data when I navigate from *.cshtml razor page to a razor component *razor using LOcalDirect. I have to use LOcalDirect because NavigateTo does not work and so doesn't the RedirectToPage or Redirect.

I thought I would use TempData but does not work in Razor Component (*.razor)

ANy help would be appreciated.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,672 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 76,276 Reputation points Moderator
    2023-11-28T02:28:26.83+00:00

    Tempdata is stored in a cookie, and deleted on the next request. But as explained in your previous post, the redirect to the page hosting the blazor component will use a JavaScript and a websocket to open the blazor circuit (so this is an additional request, plus websockets don’t support cookies).

    You could render the value (encrypted) to the hosting page and use js interop to fetch the value. You could store in a js object, hidden field or as a url parameter.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.