"REST API客户端"在Visual Studio中选择ASP.NET Core项目? [英] "REST API Client" option in Visual Studio for ASP.NET Core projects?

查看:57
本文介绍了"REST API客户端"在Visual Studio中选择ASP.NET Core项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个ASP.NET REST API并在Azure中运行.从Visual的一个较旧的.NET 4.5项目中,我已经使用以下菜单选项生成了一个客户端:

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:

但是当我创建一个新的ASP.NET Core(ASP.NET 5)项目并想要生成客户端时,没有这样的选择:

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

在ASP.NET Core项目中为我的REST api生成客户端的预期方法是什么?

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

推荐答案

在ASPNET Core 1.0上,方法(至少现在,情况可能会发生变化)是使用摇晃来生成REST API文档,一旦完成,您就可以使用 AutoRest以自动生成多种语言的客户端.

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.

要在Core App中使用Swagger,请添加您的 projects.json 文件:

To use Swagger in a Core App, add in your projects.json file:

"dependencies": {
    ...
    "Swashbuckle": "6.0.0-rc1-final"
},

然后在Startup.cs文件中,添加初始化:

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 将在 http://yourdomain/swagger/ui/index.html 中生成具有人类可读"内容的URL. UseSwaggerGen 会在 http://yourdomain/swagger/v1/swagger.json 中生成 swagger.json 文件.

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.

最后,您需要装饰一下方法,以通过添加以下内容来告诉Swagger它们提供哪种输出(自动检测输入):

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.

更新:要使用AutoRest生成客户端,只需转到命令提示符(安装了AutoRest)并浏览到您的项目文件夹,然后键入:

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

这将在项目内部创建一个"Generated"文件夹,其中包含所有文件和一个代理类,您甚至可以在Startup.cs文件中使用它并定义依赖项注入.

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"));
});
}

这篇关于"REST API客户端"在Visual Studio中选择ASP.NET Core项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆