I have an ASP.NET application. The Index method that loads the page performs queries on a database. These queries are expensive and that datasets change infrequently, so this is a perfect use case for caching.
I added the output caching middleware to my Program.cs
file.
I want to cache the server-side output, so I added flags to my Index functions:
[HttpGet]
[OutputCache(Duration = 43200, NoStore = false)]
public IActionResult Index()
{
some code...
}
When I access the page using: https://web.app.com/Controller/Index
the page caches as expected. However, when I use: https://web.app.com/Controller/
(which routes to the same endpoint) the page fails to cache, even after refreshing.
I understand that each unique URL should be cached by the server. Is there any reason why the root URL is being ignored?
2
Answers
I changed my app setup in
Program.cs
from:to:
For some reason, this fixed the problem--the root URL for the controller is now being cached.
Microsoft documentation states that
MapDefaultControllerRoute()
is a convenience method that is equivalent to the first code snippet.If anyone knows why this would make a difference, feel free to comment.
Perhaps the reason is ‘/’ character in the end of "https://web.app.com/Controller/". Try to get rid of it or use redirection: