0

Sorry guys this may seem like a novice question but I've never worked with endpoint mapping before and just can't seem to figure it out.

So I wrote a service which listens for requests, the body of said request contains a json object which my service needs to properly process the requests. While I know how to route those requests to the appropriate controller I just don't know how I am supposed to read and deliver the request body to the controller.

Startup.cs

class Startup
    {
        private readonly IHostingEnvironment _env;
        private readonly IConfiguration _config;
        private readonly ILoggerFactory _loggerFactory;

        public Startup(IHostingEnvironment env, IConfiguration config,
            ILoggerFactory loggerFactory)
        {
            _env = env;
            _config = config;
            _loggerFactory = loggerFactory;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRouting();
            services.AddMvc();

        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseMvc(routes =>
            {
                routes.MapRoute("default", "{controller=Classifier}/{action}");
            });
        }
    }

Controller

 public class ClassifierController : Controller
    {
        private Classifier documentClassifier = new Classifier();

        public TrainingResponse Training(RequestTrainingData requestDataTrain)
        {
            return documentClassifier.Train(requestDataTrain);
        }

        public PredictionResponse Predict(RequestPredictionData requestDataPredict)
        {
            return documentClassifier.Predict(requestDataPredict);
        }
    }

As you can see I expect either a Object of type RequestPredictionData or RequestTrainingData:

RequestPredictionData

    public class RequestPredictionData
    {
        public string Principal { get; set; }
        public IEnumerable<PredictionInputDocument> Docs { get; set; }
    }

RequestTrainingData

    public class RequestTrainingData
    {
        public string Principal { get; set; }
        public IEnumerable<TrainingDocument> Docs { get; set; }
    }

Sorry for the rather long text and thanks to anyone that took the time to read my question^^'

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Proximus
  • 5
  • 1

1 Answers1

0

It seams like you need a Web API. Like this:

Please use WebApi configuration:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<TodoContext>(opt =>
           opt.UseInMemoryDatabase("TodoList"));
        services.AddControllers();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

Then you shoud mark your controller by atribure

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase

And mark your methods as POST

[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ActionResult<Pet> Create(Pet pet)
{
    pet.Id = _petsInMemoryStore.Any() ? 
             _petsInMemoryStore.Max(p => p.Id) + 1 : 1;
    _petsInMemoryStore.Add(pet);

    return CreatedAtAction(nameof(GetById), new { id = pet.Id }, pet);
}

So, you can read mo here: https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.0&tabs=visual-studio

https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.0

TemaTre
  • 1,422
  • 2
  • 12
  • 20