Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"REST API Client" option in Visual Studio for ASP.NET Core projects?

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:

Old .NET 4.5 project

But when I create a new ASP.NET Core (ASP.NET 5) project, and want to generate the client there is no such option:

New ASP.NET Core project

What is the intended way to generate the client for my REST api in ASP.NET Core projects?

like image 826
Frode Lillerud Avatar asked Mar 08 '16 10:03

Frode Lillerud


People also ask

How do I add a client to rest API?

Now, right-click on the project and select “Add” and click on the “REST API Client” option. On selecting REST API Client, it will open the tool where you can either provide Swagger Url or you can select Swagger metadata file from your local drive.

How do I create a REST API in NET Core?

Create an ASP.NET Core REST API application Step 1: Go to File > New, and then select Project. Step 2: Choose Create a new project. Step 3: Select ASP.NET Core Web Application template. Step 4: Enter the Project name, and then click Create.

How do I create a REST API project in Visual Studio?

Select the Visual C# | Web project type from the project type tree view, then select the ASP.NET MVC 4 Web Application project type. Set the project's Name to ContactManager and the Solution name to Begin, then click OK. In the ASP.NET MVC 4 project type dialog, select the Web API project type. Click OK.

What is REST API in .NET Core?

A REST API can hide the complexity behind large scale solutions using simple verbs like POST, PUT, or PATCH. In this article, Camilo Reyes explains how to create a REST API in . NET Core. One way to scale large complex solutions is to break them out into REST microservices.


2 Answers

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<IYourApi>(provider =>
{
    return new YourAPI(new Uri("http://yourapi"));
});
}
like image 68
Matias Quaranta Avatar answered Sep 21 '22 15:09

Matias Quaranta


I also had this problem so I built a tool called REST API Client Code Generator to solve it. I worked in teams where we used tools like AutoRest, NSwag, and Swagger Codegen to generate our REST API Clients and consume it from .NET Core web applications. It always annoyed me that the "Add New - REST API Client..." tooling in Visual Studio didn't always work and was very troublesome when it was time to re-generate the client

Add New REST API Client

Add New REST API Client Dialog

This would add the OpenAPI specification file (Swagger.json) to the project and set a custom tool so that every time changes are made to it the REST API Client code is re-generated. You can also right click on the Swagger.json file and switch code generators

Re-generate or Switch Code Generator

And for NSwag Studio files you can also just right click and re-generate

Generate NSwag Studio Output

I built the tool mainly for personal use and for use within my teams but if you find it useful and think it lacks something you really need then please reach out

like image 35
Christian Resma Helle Avatar answered Sep 25 '22 15:09

Christian Resma Helle