I\'ve got a ASP.NET REST API up and running in Azure. From an older .NET 4.5 project in Visual I\'ve generated a client using this menu option:
But when I creat
On ASPNET Core 1.0 the approach (at least right now, things might change) is to use Swagger to generate the REST API documentation and once you did that, you can use AutoRest to automatically generate a client in several languages.
To use Swagger in a Core App, add in your projects.json
file:
"dependencies": {
...
"Swashbuckle": "6.0.0-rc1-final"
},
Then in your Startup.cs file, you can add the initialization:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//other uses
//this generates the swagger.json file
app.UseSwaggerGen();
//this is optional, it will generate a visual website for your documentation
app.UseSwaggerUi();
}
UseSwaggerUi
will generate an URL with "human-readable' content in http://yourdomain/swagger/ui/index.html
. UseSwaggerGen
will generate the swagger.json
file in: http://yourdomain/swagger/v1/swagger.json
.
Finally, you need to decorate your methods to tell Swagger what kind of Output do they offer (the Input is autodetected), by adding something like:
[Produces(typeof(MyItemClass))]
[SwaggerResponse(System.Net.HttpStatusCode.OK, Type = typeof(MyItemClass))]
[HttpGet("{id}")]
public IActionResult Get(string id)
{
if (string.IsNullOrEmpty(id))
{
return HttpBadRequest();
}
var item = _service.GetRecord(id);
if (item == null)
{
return HttpNotFound();
}
return new ObjectResult(item);
}
Hope it helps clearing things up.
UPDATE: To generate the client with AutoRest just go to the command prompt (with AutoRest installed) and browse to your project folder, then type:
autorest -Namespace YourDesiredNamespace -Input http://yourapi/swagger/v1/swagger.json
This will create a "Generated" folder inside your project with all the files and a proxy class you can even use in your Startup.cs file and define Dependency Injection.
public void ConfigureServices(IServiceCollection services)
{
//....
services.AddSingleton(provider =>
{
return new YourAPI(new Uri("http://yourapi"));
});
}