Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core RESTful, same routes but different actions

I try to have multiple routes with equal URLs like files/{fileId} but different return values. I would like to distinguish the routes based on the MIME type. In one case I want the file details in JSON format and in the other the file itself.

/// <summary>
    /// get a file by its Id
    /// </summary>
    /// <response code="200">The file for download</response>
    /// <response code="404">File id was not found</response>
    [HttpGet("files/{fileId}")]
    public async Task<IActionResult> GetFile([FromRoute] int fileId)
    {
        var fileModel = await _boFileService.GetFileStreamAndFileName(fileId);
        return File(fileModel.Stream, MediaTypeNames.Application.Octet, fileModel.FileName);
    }

    /// <summary>
    /// get the details of a file by its Id
    /// </summary>
    /// <response code="200">The details for the file, like name, status and type</response>
    /// <response code="404">File id was not found</response>
    [HttpGet("files/{fileId}")]
    public async Task<ActionResult<BoFileModel>> GetFileDetails([FromRoute] int fileId)
    {
        return await _boFileService.GetFileDetails(fileId);
    }

At the moment I got the following error:

Actions require a unique method/path combination for Swagger/OpenAPI 3.0

Has anybody an idea? Is it possible to have equal routes?

Thank you

like image 924
Yannick Sutter Avatar asked Oct 28 '25 00:10

Yannick Sutter


1 Answers

You should be able to use the ConsumesAttribute to describe what content types maps to which controller action.

like image 102
davidfowl Avatar answered Oct 30 '25 16:10

davidfowl