I get a 404 error when I`m trying to post JSON data on https://centroban.herokuapp.com/api/callback . It is written on c# and razor pages. Can you help me, please
This is Callback.cs file
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using VkNet.Abstractions;
using VkNet.Model;
using VkNet.Model.RequestParams;
using VkNet.Utils;
namespace Centroban
{
[Route("api/[controller]")]
[ApiController]
public class CallbackController : ControllerBase
{
/// <summary>
/// Конфигурация приложения
/// </summary>
/// private readonly IConfiguration _configuration;
private readonly IVkApi _vkApi;
private readonly IConfiguration _configuration;
public CallbackController(IVkApi vkApi, IConfiguration configuration)
{
_vkApi = vkApi;
_configuration = configuration;
}
[HttpPost]
public IActionResult Callback([FromBody] Updates updates)
{
// Проверяем, что находится в поле "type"
switch (updates.Type)
{
// Если это уведомление для подтверждения адреса
case "confirmation":
// Отправляем строку для подтверждения
return Ok(_configuration["Config:Confirmation"]);
case "message_new":
{
// Десериализация
var msg = Message.FromJson(new VkResponse(updates.Object));
// Отправим в ответ полученный от пользователя текст
_vkApi.Messages.Send(new MessagesSendParams
{
RandomId = new DateTime().Millisecond,
PeerId = msg.PeerId.Value,
Message = msg.Text
});
break;
}
}
// Возвращаем "ok" серверу Callback API
return Ok("ok");
}
}
}
and this is Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using VkNet;
using VkNet.Abstractions;
using VkNet.Model;
namespace Centroban
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddControllers();
services.AddSingleton<IVkApi>(sp =>
{
var api = new VkApi();
api.Authorize(new ApiAuthParams { AccessToken = Configuration["Config:AccessToken"] });
return api;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
I guess there are problems with routing, but anyway, a don`t know how to deal with them. Here is my site https://centroban.herokuapp.com
Firstly,you can try to add routing for mvc:
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
And then you cannot use
https://centroban.herokuapp.com/api/callback
to call the action in browser,because it is a HttpPost
request.Here is a example to call the action in postman.