Troubleshooting swagger generation in webapi

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

When working with swagger you might get following error:

"Not supported by Swagger 2.0: Multiple operations with path 'api/Values' and method 'GET'”.

This issue can be solved in two ways:

Solution I

Use attribute Route.

       
       
public IEnumerable<Contact
> Get()
        {
        });

           
return
list;
        }

        [
Route("api/values/{id}"
)]
       
public string Get(int
id)
        {
           
return "value :)"
;
        }


        [
Route("api/values/{id1}/{id2}"
)]
       
public string Get(int id1, int
id2)
        {
           
return "value :)"
;
        }



 

Solution II

Implement IOperationFilter

    public class ResolveMultipleOperationsIssue : IOperationFilter
    {
       
public void Apply(
           
Operation
operation,
           
SchemaRegistry
schemaRegistry,
           
ApiDescription
apiDescription)
        {
           
if (operation.parameters != null
)
            {
                operation.operationId +=
"By"
;
               
foreach (var parm in
operation.parameters)
                {
                    operation.operationId +=      
                    string.Format("{0}", parm.name) ;
               
                }
           
           
}
        }
    }


Additionally you have to register the filter in SwaggerConfig.xml.


c.OperationFilter<ResolveMultipleOperationsIssue>();


And here is the result

image


Posted Nov 26 2015, 06:29 PM by Damir Dobric
developers.de is a .Net Community Blog powered by daenet GmbH.