d00d
d00d

Reputation: 763

OnPostAsync not called

I try to implement a E-Mail sender in my Blazor server-side app. I'm following this example:

https://kenhaggerty.com/articles/article/aspnet-core-22-smtp-emailsender-implementation

I have the view Home.cshtml

@page "/Home"
@model UserManagement.HomeModel
@{
    ViewData["Title"] = "Home";

}

<div class="row">
    <div class="col-6">
        <label class="alert alert-success">@Model.EmailStatusMessage</label>
    </div>
</div>
<div class="row">
    <div class="col-6">
        <form method="post">
            <div class="form-group">
                <label asp-for="Email"></label>
                <input asp-for="Email" class="form-control" />
                <span asp-validation-for="Email" class="text-danger"></span>
            </div>
            <button type="submit" class="btn btn-primary">Email Test</button>
        </form>
    </div>
</div>

And the Home.cshtml.cs

 public class HomeModel : PageModel
    {
        private readonly IEmailSender _emailSender;

        public HomeModel(IEmailSender emailSender)
        {
            _emailSender = emailSender;
        }

        public string EmailStatusMessage { get; set; }

        [Required]
        [BindProperty]
        public string Email { get; set; }

        public void OnGet()
        {
        }

        public async Task OnPostAsync()
        {
            if (!ModelState.IsValid)
            {

            }

            var email = Email;

            var subject = "Email Test";

            var message = "This is a test message.";

            await _emailSender.SendEmailAsync(email, subject, message);

            EmailStatusMessage = "Send test email was successful.";


        }
    }
}

When I hit the button, I receive a 400 error and the debugger never stops at my breakpoint which I have set in the OnPostAsync method. I'm a little confused why that never happens.

Can you solve this mystery?

Upvotes: 1

Views: 5982

Answers (3)

Syed Hamza G
Syed Hamza G

Reputation: 21

please use HttpPost in your handler [HttpPost] public async Task OnPostAsync() {}

Upvotes: 1

Yahir Vite Lugardo
Yahir Vite Lugardo

Reputation: 83

You have to add "_ViewImports.cshtml" file with the following "usings"

@using Microsoft.AspNetCore.Identity @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Then add to this file the namespaces you'll be using on thosse pages

Upvotes: 5

d00d
d00d

Reputation: 763

I think I stumbled across another post here where a similar issue was discussed. The post of enet explains it:

Server-side Blazor page post to Razor page http error 400

I quote:

The method="post" is not applicable in Blazor or any other SPA frameworks. In Blazor there is special code that prevent a post back. Of course you can force a postback, which means that your Blazor page expires, and if you try to return to it, it is rerendered, and all your previous state is lost.

What you're trying to do here is to get the user's credentials in a Blazor component, and then try to post the data to a Razor Page Model for validation. You can't do that, and you should not do that. You should redirect your user to a login page in which he enters his credentials and those credentials should be validated against some sort of store, say SqlServer database, and then returns to the Blazor app. Remember, the redirection to a Razor Page means that your Blazor app has expired. One again you do not post the user's credentials to the Login Razor Page. The user should enter his his credentials on Login.cshtml

Upvotes: 2

Related Questions