Click here to Skip to main content
15,878,231 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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").

C#
[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:
C#
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:
C#
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
C#
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.
Posted
Updated 3-Aug-22 21:05pm
v2
Comments
Afzaal Ahmad Zeeshan 3-Aug-22 19:15pm    
Best case is to debug the application and check what values do these parameters have. I am guessing they are either null, or they do not contain a valid URI (thus, the error).

1 solution

If you debug, you should observe that the URL formed is not a valid one.

A URI is a compact representation of a resource available to your application on the intranet or internet.. Refer: Uri Class (System) | Microsoft Docs[^]

Looking at what your error means: UriFormatException Class (System) | Microsoft Docs[^]
Quote:
The UriFormatException is thrown by the Uri class constructor if the supplied URI could not be correctly parsed. The format for a valid URI is defined in RFC 2396.


Check specifically for the below case mentioned here[^] that happens quite frequent (I understand this is not your API call but the underlying logic remains the same):
Quote:
If the BaseAddress property is not an empty string ("") and address does not specify an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900