Say I had this page in ASP.NET RazorPages:
@{
SocketGuild guild = (SocketGuild) ViewData["guild"]
}
<p>The guild has @(guild.Roles.Count) roles.</p>
public async Task OnGet()
{
ViewData["guild"] = GetGuild();
}
public async Task OnPost()
{
Console.WriteLine("Post Received");
}
When a GET request is received, ViewData["guild"] is set by the OnGet handler and the .cshtml renders, allowing the page to be returned.
When a POST request is received the .cshtml file is rendered but a null reference exception is thrown because ViewData["guild"] has not been defined. I don't need my .cshtml file to be rendered on a post because my requests are made in the background.
Is it possible to only render the .cshtml file if the OnGet method has been executed? I'm willing to switch to MVC style handlers if necessary.