You forgot the "api" in the URL (route) api/SMS/Send. The default Web API template in .NET 6 opens swagger which is a utility for test Web API. You can also use swagger to get the URL.
Unable to access my 1st Web API
Hi All,
I don't understand why I'm unable to access my 1st web API (Net core 6)?
POST https://localhost:7069/sms/send
{
"username":"dummy",
"password":"hELooDummy..",
"sms_text":"Hello World",
"sms_recipient":"62818122334"
}
it returns: 404
[Route("api/[controller]")]
[ApiController]
[HttpPost]
[Route("api/SMS/Send")]
[AllowAnonymous]
public async Task<bool> SendAsync([FromBody] JObject jo)
{
bool ret = false;
string apiKey = Request.Headers["api_key"].ToString();
string apiSecret = Request.Headers["api_secret"].ToString();
if (apiKey != Models.AppSettings.Application.API_Key || apiSecret != Models.AppSettings.Application.API_Secret)
{
return await Task.FromResult(ret);
}
string apiUsername = (string)jo["username"];
string apiPassword = (string)jo["password"];
string smsText = (string)jo["sms_text"];
string smsRecipient = (string)jo["sms_recipient"];
try
{
}
catch (Exception e) { await Helper.SaveLogAsync("SMSController.SendAsync: " + e.ToString()); }
finally { }
return await Task.FromResult(ret);
}
my program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
if (!builder.Environment.IsDevelopment())
{
builder.WebHost.UseKestrel(serverOptions =>
{
serverOptions.ListenAnyIP(7069);
});
}
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default1",
pattern: "api/{controller}/{action}");
);
app.Run();
Developer technologies ASP.NET ASP.NET Core
3 answers
Sort by: Most helpful
-
-
winanjaya 146 Reputation points
2022-10-26T11:19:44.763+00:00 @AgaveJoe
thank you so much for your reply, really appreciate it.but I also had tried /api/sms/send
but in Swagger it becomes /api/SMS/api/sms/send
what did I do wrong?
btw, I also had changed program.cs to use
app.MapControllers();
but no luck . need your help
thank you
-
Anonymous
2022-10-27T03:06:31.173+00:00 Hi @winanjaya ,
To make attribute routing less repetitive, route attributes on the controller are combined with route attributes on the individual actions. Any route templates defined on the controller are prepended to route templates on the actions. Placing a route attribute on the controller makes all actions in the controller use attribute routing.
So, when you use the Route attribute as below, the request URL is:
https://localhost:{port}/api/SMS/api/SMS/Send
:To prevent the route attributes combined, you can add a
/
or~/
before the route attribute in the action methods, like this:[Route("/api/SMS/Send")]
or[Route("~/api/SMS/Send")]
, then the request url is:https://localhost:{port}/api/SMS/Send
:To make the request URL is
https://localhost:{port}/SMS/Send
, you can change the route attribute to[Route("/SMS/Send")]
or[Route("~/SMS/Send")]
.Besides, you can also use Http verb attributes to set the route, like this: use
[HttpPost("/sms/send")]
, the request URL is:https://localhost:{port}/smd/send
More detail information, see Combining attribute routes and Attribute routing with Http verb attributes.
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,
Dillion