0

I am confused on how to call or create server/controller methods in razor pages. When I do MVC, I would create methods in the controller and would use an anchor tag to call it like this:

<a href="/Security/LogOut/">

How do I do this in razor pages? I ended up creating a new page name "SignOut" and implemented the login inside the OnGet and used an anchor tag like this:

<a href="/SignOut">

What if I have more than one action method I want to group in a file? Do I need to create a page for every action?

Arcadian
  • 4,312
  • 12
  • 64
  • 107
  • 1
    @Url.Action("Action", "Controller") is probably what you are looking for but its a little difficult to understand exactly what you are asking. This SO post may be of interest to you https://stackoverflow.com/questions/7709001/html-actionlink-vs-url-action-in-asp-net-razor – justsomeguy Aug 23 '18 at 16:31
  • No. That is the old MVC stuff. I am referring to Core 2.1 / Razor Pages – Arcadian Aug 23 '18 at 16:56
  • https://stackoverflow.com/questions/47767851/url-action-in-asp-net-core – justsomeguy Aug 30 '18 at 17:54

1 Answers1

0

You've probably long since found the answer you were looking for. For others that see this question, you can use:

<a asp-page="/SignOut">Sign Out</a>

In the case of Razor Pages, the GET and POST methods are handled by each code-behind file. For instance, for the Razor Page Details.cshtml, there is a code-behind file called Details.cshtml.cs that defines OnPostAsync and OnGetAsync which handle POST and GET requests respectively. For more information, see:

Microsoft Razor Pages Tutorial

Matthew Miller
  • 555
  • 3
  • 15