Try our conversational search powered by Generative AI!

Why is my custom admin menu item generating two requests when clicked?

Vote:
 

I've implemented a simple menu provider that looks like this:

[MenuProvider]
public class CustomAdminMenuProvider : IMenuProvider
{
	public IEnumerable<MenuItem> GetMenuItems() => new List<MenuItem>
	{
		new SectionMenuItem("Custom Tools", $"{MenuPaths.Global}/cms/admin/customtools"),
		new UrlMenuItem("Log Settings", $"{MenuPaths.Global}/cms/admin/customtools/logsettings", "/customadmin/logsettings")
	};
}


Behind /customadmin/logsettings is an equally simple Razor page with a PageModel that looks like this:

public class LogSettingsModel : PageModel
{
	public LogLevel LogLevel { get; set; }

	public void OnGet()
	{
		LogLevel = SettingsHelper.LogLevel;
	}
}


The two menu items specified by the menu provider are displayed as expected, but when I click on the Log Settings menu item, the OnGet() method is called twice. What could I be doing wrong?

#311420
Oct 25, 2023 10:04
Vote:
 

Does the OnGet get called twice only when clicking the menu or does it also do this when you reload the Log settings page?

#311482
Oct 26, 2023 7:27
Vote:
 

It is only when clicking the menu that OnGet is called twice. When reloading it is called only once.

#311492
Oct 26, 2023 9:18
Vote:
 

It might occurs beacuse you have multiple routes configured for the same URL. This can cause the OnGet method to be called twice if the routing system matches multiple routes to the URL. You should check your Startup.cs for any route configuration that might be conflicting or creating multiple routes to the same page.

#311692
Oct 30, 2023 8:14
Vote:
 

Thanks Isac. We just have a few MapControllerRoute directives for robot.txt etc. in Startup.cs, which I don't think should have any impact in this case. Just in case I commented these lines of code out, but with unchanged behavior. No, I think the weirdness is happening on the client. A peek in the Network tab under the browser's DevTools reveals that there are two requests for the same document. The first request has the following comment: "The request status cannot be shown here because the page that issued it unloaded while the request was in flight. ..."

#311704
Oct 30, 2023 15:28
Vote:
 

Hmm... It works, i.e. I only get one request, if I remove all event handlers from the anchor element of my menu item. So, if I only could get a JavaScript loaded with the admin UI I would thus have a fix, albeit a very ugly one... 🤔

#311932
Edited, Nov 03, 2023 15:01
Vote:
 

For the time being I've settled for a another hack: I use a lock and a (short-lived) memory cache so that the seconds request waits for the first request to generate a database result that it can reuse. It is not good solution and I don't like it, but it solves my main performance issue. 🤷‍♂️

#312890
Nov 21, 2023 15:50
Ted - Nov 23, 2023 12:16
For that type of cache scenario, you may want to use ReadThrough: https://world.optimizely.com/blogs/Johan-Bjornfot/Dates1/2018/5/iobjectinstancecache-readthrough/
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.