While using ASP.Net MVC 5 in .NET Framework 4.8, I am constantly facing a 404 error due to inconsistent URL generation. For instance, when including the _PageNav.chstml partial at the top of each page and adding
@Url.Action("Index", new { controller = "Home" })
to redirect users back to the home page, it works fine within the navigation bar as <a class="navbar-brand" href="/" />
.
However, if I use the same
@Url.Action("Index", new { controller = "Home" })
later on the same page for a button, the resulting URL is different: <a id="doneButton" class="btn btn-secondary px-4" href="https://localhost:44337/smrt/">Done</a>
This inconsistency often leads to issues where AJAX JavaScript references to controllers end up with missing references like /create
, causing URLs like https://localhost:44337/create
instead of
https://localhost:44337/home/create
or /home/create
, resulting in https://localhost:44337/home/home/create
instead of https://localhost:44337/home/create
Furthermore, I have limitations due to security restrictions, meaning I cannot include any JavaScript directly on the page itself. This restricts me from writing razor code in my .cshtml files that would generate JavaScript. Instead, I can only use JavaScript sourced externally in the page files.