Hello, in my .NET6 MVC app I have created an endpoint which accepts two strings, one containing data which will be transformed into a file and and second one which takes a type of file which will we want to create (which is "pdf").
[HttpPost]
public ActionResult DownloadFile([FromForm] string data, [FromForm] string fileType)
{
try
{
if (!string.IsNullOrWhiteSpace(data))
{
return GenerateReportDocument(data, fileType);
}
}
catch (Exception ex)
{
logger.LogError(ex, $"Unexpected error occured in {nameof(DownloadFile)}.");
return NoContent();
}
return NoContent();
}
Then data is taken into GenerateReportDocument method:
private ActionResult GenerateReportDocument(string data, string fileType)
{
var specificationString = specificationGeneratorService.GenerateSpecificationString(JObject.Parse(data));
logger.LogWarning($"Check images in specificationString: '{specificationString}'");
if (string.IsNullOrWhiteSpace(specificationString))
{
specificationString = "<p></p>";
}
var reportGenerator = generateReportDocuments.SingleOrDefault(r => r.FileType.ToLower().Equals(fileType.ToLower()));
if (reportGenerator != null)
{
reportGenerator.GenerateReportDocument(SpecificationFileName, specificationString);
}
return NoContent();
}
Which then is supposed to be taken into third method:
public HttpContent GenerateReportDocument(string fileName, string specificationString)
{
var requestContent = new StringContent(JsonConvert.SerializeObject(new { Html = specificationString }));
requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var httpResponse = Flurl.Url.Combine(_abcPdfOptions.PdfConverterUrl, "pdf/convertfromhtmltopdf")
.PostAsync(requestContent).GetAwaiter().GetResult();
HttpContent httpContent = httpResponse.Content;
httpContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = $"{fileName}.{FileExt}",
};
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return httpContent;
}
But when the line
reportGenerator.GenerateReportDocument(SpecificationFileName, specificationString);
in the second method is executed there appears an error "System.UriFormatException: 'Invalid URI: The format of the URI could not be determined.'" Which causes the request to crash.
What I have tried:
I have tried editing my code and changing the methods into IAtionResult or HttpResponse, but results still remains the same. Also I have tried adding extra validation to make sure that data sent into endpoint is correct, but with no success - the error remains.