call an api in C#

Anjali Agarwal 1,366 Reputation points
2024-03-18T04:07:46.5366667+00:00

I have the following code:


namespace TwilioSendMessages.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SMSController : ControllerBase
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
        public readonly IConfiguration _configuration;
        private readonly ITwilioRestClient _client;
        public SMSController(IWebHostEnvironment webHostEnvironment, IConfiguration configuration, ITwilioRestClient client)
        {
            _webHostEnvironment = webHostEnvironment;
            _configuration = configuration;
            _client = client;
        }
          [Route("SendSMS")]
        [HttpPost]
        public IActionResult SendSMS(SmsMessage model)
        {
          
            var message = MessageResource.Create(
            to: new PhoneNumber(model.To),
            from: new PhoneNumber(model.From), 
            body: model.Message,
            client: _client); // pass in the custom client
            return Ok("Success");
        }
    }
}
namespace

when I tried to call the api on my local machine like this:



https://localhost:7262/api/SMS/SendSMS

it says the page is not working. I also tried calling the API this way. I get the same message saying the page is not working:


https://localhost:7262/api/SMSController

When I run the code directly. This page comes up:

User's image

When I run the code directly.This page comes up and I can type "To" , "From" and "Message" and it works. How can I call this API using the URL on my local machine.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,167 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
297 questions
0 comments No comments
{count} votes

Accepted answer
  1. JasonPan - MSFT 4,281 Reputation points Microsoft Vendor
    2024-03-18T07:52:43.7266667+00:00

    Hi @Anjali Agarwal,

    It's a post request. We can't make it works in browser. If you want to test it in web page, you have to create a html, and invoke this SendSMS function in javascript by using post method.

    Another choice, you can use the tools like Postman to test your api.


    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

    Jason

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful