I am writing a new Razor Pages application (this is my first time with Razor pages rather than MVC), and I came to the point where I was needing to create a link and for the life of me I could not figure out why it was not working even though I tried every single variation of the parameters to the ActionLink method that I could think of. I then instead used the Anchor Tag Helper methodology instead with the exact smae parameters and it worked beautifully. I was already confused when I scaffolded my views because the code generation uses both the Tag Helpers and the Html Helpers where as my brain tells me you should really pick one or the other and try to be as consistent as possible.
The code in the cshtml file is as follows:
<a asp-page="./DeleteIngredient" asp-route-MenuItemId="@item.MenuItemId" asp-route-IngredientId="@item.IngredientId">Delete</a>
@Html.ActionLink("Delete", "DeleteIngredient", new { MenuItemId = @item.MenuItemId, IngredientId = @item.IngredientId })
@Html.ActionLink("Delete", "./DeleteIngredient", new { MenuItemId = @item.MenuItemId, IngredientId = @item.IngredientId })
The Tag Helper correctly generates the following url:
The Html Helpers generate the following:
Note: the links are on a page with the URL: https://localhost:44308/MenuItems/Details/c469f993-9cac-4adc-bf63-2aba9be249c9
In summary, am I doing something wrong? Or does this appear to be a bug in the asp net core Html.ActionLink method?
Html.ActionLink
is for MVC pattern, it requires Action and Controller names. Since you are using razor pages it will not work because you don't have controllers and action but you have Pages! So, your first approach is correct.
As an alternative you may use Url.Page
to generate the link as below:
<a href="@(Url.Page("./DeleteIngredient", null, new { MenuItemId = item.MenuItemId, IngredientId = item.IngredientId })">Delete</a>