ASP.NET Core MVC pass a url parameter to the POST

David Thielen 2,376 Reputation points
2024-05-16T21:33:20.59+00:00

Hi all;

I have a Blazor Server app. It uses the ASP.NET Identity Library which is ASP.NET Core MVC. I am trying to pass a parameter to register so a URL like /identity/account/Register?follow=uniqueId gives me the parameter follow=uniqueId.

In Register.cshtml.cs (class RegisterModel) I have:

public async Task OnGetAsync(string returnUrl = null, string following = null)
{
    ReturnUrl = returnUrl;
    Following = following;
    ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
}

public async Task<IActionResult> OnPostAsync(string returnUrl = null, string following = null)
{
    // ...
}

The value from the url is passed in to OnGetAsync() fine. But when OnPostAsync() is called, it passes in a null for following and the object property Following is also null.

How do I get that parameter in OnPostAsync()?

thanks - dave

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,256 questions
{count} votes

Accepted answer
  1. Ping Ni-MSFT 2,405 Reputation points Microsoft Vendor
    2024-05-17T02:15:03.8366667+00:00

    Hi @David Thielen,

    In your scenario, the parameter following is present in the GET request but is lost during the POST request. This typically happens because POST requests do not automatically include URL parameters unless explicitly handled.

    One way is that pass the Following property value to asp-route-following:

    <form id="registerForm" asp-route-returnUrl="@Model.ReturnUrl" method="post" asp-route-following="@Model.Following">
    

    Another way is you can set a hidden input for Following :

    <form id="registerForm" asp-route-returnUrl="@Model.ReturnUrl" method="post" >
        <h2>Create a new account.</h2>
        <hr />
        <input type="hidden" asp-for="Following" />
        <div asp-validation-summary="ModelOnly" class="text-danger" role="alert"></div>
        <div class="form-floating mb-3">
            <input asp-for="Input.Email" class="form-control" autocomplete="username" aria-required="true" placeholder="name@example.com" />
            <label asp-for="Input.Email">Email</label>
            <span asp-validation-for="Input.Email" class="text-danger"></span>
        </div>
        <div class="form-floating mb-3">
            <input asp-for="Input.Password" class="form-control" autocomplete="new-password" aria-required="true" placeholder="password" />
            <label asp-for="Input.Password">Password</label>
            <span asp-validation-for="Input.Password" class="text-danger"></span>
        </div>
        <div class="form-floating mb-3">
            <input asp-for="Input.ConfirmPassword" class="form-control" autocomplete="new-password" aria-required="true" placeholder="password" />
            <label asp-for="Input.ConfirmPassword">Confirm Password</label>
            <span asp-validation-for="Input.ConfirmPassword" class="text-danger"></span>
        </div>
        <button id="registerSubmit" type="submit" class="w-100 btn btn-lg btn-primary">Register</button>
    </form>
    

    Register.cshtml.cs

     [BindProperty]
     public string Following { get; set; }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best regards,
    Rena

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 58,441 Reputation points
    2024-05-17T00:38:56.7366667+00:00

    In the html form action attribute, specify url path include the query string.

    <form method="post" action="@($"/identity/account/Register?follow={uniqueId}")">
    

  2. Can Kucukgultekin 0 Reputation points
    2024-05-17T20:53:31.9866667+00:00

    In ASP.NET Core MVC, URL parameters are passed to the GET method but are not automatically included in the POST request. To retain URL parameters across requests, you need to include them as hidden form fields in your form. Here's how you can modify your code to achieve this:

    1. Update the OnGetAsync method to set the Following property.
    2. Add hidden fields for ReturnUrl and Following in your form.
    3. Ensure the OnPostAsync method captures these values.

    Here's how you can do it:

    Update OnGetAsync in Register.cshtml.cs

    public async Task OnGetAsync(string returnUrl = null, string following = null)
    {
        ReturnUrl = returnUrl;
        Following = following;
        ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
    }
    public async Task<IActionResult> OnPostAsync(string returnUrl = null, string following = null)
    {
        // Your existing post logic here
        // You can now use the `following` parameter
    }
    

    Update the Register.cshtml to include hidden fields

    @page @model YourNamespace.RegisterModel @{ ViewData["Title"] = "Register"; } <h1>@ViewData["Title"]</h1> <form method="post"> <input type="hidden" name="returnUrl" value="@Model.ReturnUrl" /> <input type="hidden" name="following" value="@Model.Following" /> <!-- Your existing form fields here --> <button type="submit" class="btn btn-primary">Register</button> </form>
    

    With these hidden fields in place, when the form is submitted, the values of returnUrl and following will be included in the POST request and can be captured by the OnPostAsync method.

    By including the hidden fields, the values from the URL parameters are preserved and passed along with the form submission, allowing the OnPostAsync method to access them.