Hi anjali600,
Please refer below sample.
Model
public class SmsMessage
{
public Int64 To { get; set; }
public Int64 From { get; set; }
public string Message { get; set; }
}
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(SmsMessage model)
{
string apiUrl = "https://localhost:7078/api/SMS/SendSMS";
string inputJson = JsonConvert.SerializeObject(model);
using (HttpClient client = new HttpClient())
{
HttpContent inputContent = new StringContent(inputJson, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(apiUrl, inputContent).Result;
}
return View();
}
}
API Controller
[Route("api/[controller]")]
[ApiController]
public class SMSController : ControllerBase
{
private readonly IWebHostEnvironment _webHostEnvironment;
public readonly IConfiguration _configuration;
public SMSController(IWebHostEnvironment webHostEnvironment, IConfiguration configuration)
{
_webHostEnvironment = webHostEnvironment;
_configuration = configuration;
}
[Route("SendSMS")]
[HttpPost]
public IActionResult SendSMS(SmsMessage model)
{
return Ok("Success");
}
}
View
@model WebAPI_Core_7.Models.SmsMessage
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Index">
<span>To:</span>
<input type="text" name="To" asp-for="To" />
<br />
<span>From:</span>
<input type="text" name="From" asp-for="From" />
<br />
<span>Message:</span>
<input type="text" name="Message" asp-for="Message" />
<br />
<input type="submit" value="Submit" />
</form>
@ViewBag.Message
</body>
</html>
Program.cs
var builder = WebApplication.CreateBuilder(args);
// Enabling MVC
builder.Services.AddControllersWithViews();
var app = builder.Build();
app.UseDeveloperExceptionPage();
//Configuring Routes
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Screenshot
The Form
Recieved Values in API