Pepe
Pepe

Reputation: 93

New Api return Internal Server Error

I create asp.Net Core API like, after that I create a controller like this:

 [Route("api/Email")]
    public class EmailController : Controller
    {
        private readonly IEmailSender _emailSender;


        public EmailController(IEmailSender emailSender)
        {
            _emailSender = emailSender;

        }

            [Route("Send")]
            [HttpPost]
            public IActionResult Index(string Email)
            {
                _emailSender.SendEmailAsync(Email, "Confirm your account","correo desde api");
                return View();
            }

Problem occcurs when I try to execute this path with Postman as:

http://localhost:3703/api/Email/Send

with JSON raw parameter like:

{
    "Email":"[email protected]"
}

It just returns 500 Internal Server Error, I try to put breakpoint into api Send method and it never hitted. I need to do another configuration to execute a simple method of controller? Regards

Upvotes: 0

Views: 321

Answers (1)

CodeNotFound
CodeNotFound

Reputation: 23200

Based on the comment to your question the constructor of your controller is not hitted.

So you need to make sure that the IEmailSender is correctly configured for dependency injection.

So in your Startup class go to ConfigureServices method and add the following line if it not exists:

services.AddScoped<IEmailSender, MyEmailSenderImplementation>();

MyEmailSenderImplementation should be the class that impelments IEmailSender interface.

Upvotes: 2

Related Questions