Calling an endpoint function that accepts multipart formdata from another function in the same controller

If I have an endpoint which accepts multipart/form-data and I want to call that function from another function within the same controller, is there a way to pass that form-data without essentially setting up an HTTP post request? The issue is that the function isn’t set up to take the form-data as an argument, and is instead fetched from the request itself.

[HttpPost]
[Route("ExistingReportSubmission")]
[Consumes("multipart/form-data")]
public async Task<IActionResult> ExistingReportSubmission(CancellationToken cancellation)
{
    var form = await Request.ReadFormAsync(cancellation);
    form.TryGetValue("reportRequestId", out var reportRequestId);
    form.TryGetValue("radiologistId", out var radiologistId);
Controller() {
    ExistingReportSubmission(...) {
        var form = ...;
    
        reportSubmissionService.ReusableBusinessLogicThatDealsWithYourForm(form)
    }

    YourOtherFunction() {
        reportSubmissionService.ReusableBusinessLogicThatDealsWithYourForm(form)
    }
}

// Create a new class responsible for dealing with report submission
// Ideally, you dont want to include all your business logic in controller endpoints. This makes it less re-usable. Beside your need to call it on another endpoint, imagine having a scheduled job that also uses the
same business logic. Having a "service" class that handles report submissions means you can use dependency injection and re-use all that wonderful logic. 

ReportSubmissionService() {

    public ReusableBusinessLogicThatDealsWithYourForm(Form form) {
    // Migrate the code from ExistingReportSubmission(...) to here that you also want to use in your other function. 


    }
}

3 Likes