Try our conversational search powered by Generative AI!

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


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

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?

Oct 25, 2023 10:04

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

Oct 26, 2023 7:27

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

Oct 26, 2023 9:18

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.

Oct 30, 2023 8:14

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. ..."

Oct 30, 2023 15:28

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... 🤔

Edited, Nov 03, 2023 15:01

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. 🤷‍♂️

Nov 21, 2023 15:50
Ted - Nov 23, 2023 12:16
For that type of cache scenario, you may want to use 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.