asp.net-web-api2 相关问题

ASP.NET Web API 2是一个用于为浏览器和移动设备等客户端构建HTTP服务的框架。它基于Microsoft .NET Framework,是构建RESTful服务的理想选择。

如何使用 Swashbuckle 提供模型文档和示例值?

我使用 Web API 2 (MVC 5) 创建了一个 API 方法,如下所示: /// /// 导入给定组织的所有工作。该方法假定组织的所有... 我已经使用 Web API 2 (MVC 5) 创建了一个 API 方法,如下所示: /// <summary> /// Import all of the jobs for the given organisation. This method assumes that all of the organisation's active jobs are present in the jobs array. /// To delete a job, simply exclude it from the jobs array. To delete all of the jobs, pass an empty array /// </summary> /// <param name="org">Organisation Id, provided by Shopless team</param> /// <param name="jobs">Full list of jobs which should be imported, json array</param> /// <response code="200">Jobs list have been queued for import (includes validation errors if any)</response> /// <response code="401">Access to this organisation was denied</response> /// <response code="404">Invalid organisation id</response> [SwaggerResponse(HttpStatusCode.BadRequest)] [SwaggerResponse(HttpStatusCode.NotFound)] [SwaggerResponse(HttpStatusCode.Unauthorized)] [HttpPost] [Route("org/{org}/jobs/full-import")] public IHttpActionResult FullImport(long org, [FromBody] List<JobApiDto> jobs) { if (!ModelState.IsValid) { return BadRequest("Jobs array is invalid"); } else if (org < 1) { return NotFound("Invalid organisation id"); } else if (!((ClaimsPrincipal)User).HasReadWriteAccessToOrganisation(org)) { return Unauthorized("Access to this organisation was denied"); } _apiProductUploader.Upload(org, jobs, out string message); return Accepted(message); } 上述方法接受JobApiDto的列表: public class JobApiDto { /// <summary> /// Job Id (unique id assigned to the job by the API consumer) /// </summary> /// <example>e5f52dae-e008-49bd-898b-47b5f1a52f40</example> public string UniqueThirdPartyId { get; set; } /// <summary> /// Short description which will be displayed in search result /// </summary> /// <example>Competitive salary, great team, cutting edge technology!</example> public string Synopsis { get; set; } // more properties 这就是 Swagger 文档的样子: 当我展开OrganisationJobs动作时: 这是模型选项卡: 如您所见,控制器的 xml 文档为 API 方法生成了正确的描述,但我无法理解为什么我为模型提供的描述(即 JobApiDto)没有显示? 此外,当我单击“示例值”时,什么也没有发生。 感谢亚历山大指出这个答案。 我安装了 Swashbuckle.Examples Nuget 包。 (文档很棒。) 我不得不用以下方式注释控制器: [SwaggerRequestExample(typeof(JobApiDto), typeof(ListJobApiDtoExample), jsonConverter: typeof(StringEnumConverter))] 这就是控制器定义的样子: /// <summary> /// Import all of the jobs for the given organisation. This method assumes that all of the organisation's active jobs are present in the jobs array. /// To delete a job, simply exclude it from the jobs array. To delete all of the jobs, pass an empty array /// </summary> /// <param name="org">Organisation Id, provided by Shopless team</param> /// <param name="jobs">Full list of jobs which should be imported, json array</param> [SwaggerRequestExample(typeof(JobApiDto), typeof(ListJobApiDtoExample), jsonConverter: typeof(StringEnumConverter))] [SwaggerResponse(HttpStatusCode.OK, Description = "Jobs array has been queued for import", Type = typeof(ImportResponse))] [SwaggerResponseExample(HttpStatusCode.OK, typeof(ImportResponseExamples))] [SwaggerResponse(HttpStatusCode.BadRequest, "Jobs array is invalid")] [SwaggerResponse(HttpStatusCode.NotFound, "Invalid organisation id")] [SwaggerResponse(HttpStatusCode.Unauthorized, "Access to this organisation was denied")] [HttpPost] [Route("org/{org}/jobs/full-import")] public IHttpActionResult FullImport(long org, [FromBody] List<JobApiDto> jobs) { if (!ModelState.IsValid) { return BadRequest("Jobs array is invalid"); } else if (org < 1) { return NotFound("Invalid organisation id"); } else if (!((ClaimsPrincipal)User).HasReadWriteAccessToOrganisation(org)) { return Unauthorized("Access to this organisation was denied"); } _apiProductUploader.Upload(org, jobs, out ImportResponse importResponse); return Accepted(importResponse); } 这里是ListJobApiDtoExample的实现: public class ListJobApiDtoExample : IExamplesProvider { public object GetExamples() { return new JobApiDto() { UniqueThirdPartyId = "e5f52dae-e008-49bd-898b-47b5f1a52f40", CategoryId = 1183, Title = "Senior Software Developer", Description = "Example company is looking for a senior software developer... more details about the job", Reference = "", Synopsis = "Competitive salary, great team, cutting edge technology!", MinSalary = 120000, MaxSalary = 140000, SalaryPer = "Per annum", DisplaySalary = true, CustomSalaryText = "plus bonus", WorkType = "Full time", Location = "Wellington CBD", ContactName = "John Smith", ContactEmail = "[email protected]", ContactPhoneNumber = "021 123 456 789", ApplicationUrl = "", LogoUrl = "https://www.example-company.com/my-company-logo", YouTubeVideo = "https://www.youtube.com/watch?v=khb7pSEUedc&t=1s", StartDate = null, PostedAt = null }; } } 请注意,我还有一个 API 返回类型的示例,ImportResponse。与之前的模型类似,我在控制器上有这个注释: [SwaggerResponseExample(HttpStatusCode.OK, typeof(ImportResponseExamples))] 这是植入: public class ImportResponseExamples : IExamplesProvider { public object GetExamples() { return new ImportResponse() { Message = "Products are queued to be imported" }; } } 现在,文档正确显示了示例:

回答 1 投票 0

swagger UI 在 webapi 中没有显示任何内容

我跟进到 xml 文档部分,以便使用 Swashbuckle 创建 Swagger 文档。它应该允许我通过(在我的例子中)查看端点: http://localhost:51854/swagger/ui/index

回答 6 投票 0

web api 从处理程序内部获取路由模板

在将问题放在这里之前我进行了很多搜索,但是搜索得越多,我就越困惑。 所以我创建了一个处理程序,我正在尝试获取这样的路由: 公开课

回答 5 投票 0

允许在 Web API 参数中使用特殊字符

我需要能够处理 REST 调用中的特殊字符。具体来说。和/字符。 例如,我有一个 GET 路由 /api/division/{someDivision}。现在,用参数调用这条路线...

回答 2 投票 0

为什么我的 .net Core API 属性路由不起作用?

我正在试验“Hosted Blazor WASM”项目。此问题不涉及 Blazor 客户端路由器(工作正常)。这与“服务器端”路由器无关

回答 3 投票 0

ASP.NET Web API 2 - [FromBody] 为空

我有一个非常简单的测试应用程序,可以将 json Post 发送到我的网络服务。一切正常,但由于某种原因,Post([FromBody] 字符串值)中收到的值为空。 // 发布应用...

回答 1 投票 0

从 ClaimsPrincipal 中检索/读取声明值

如果我直接进入它,我已经构建了一个具有基本身份验证的 RESTful 服务(WebAPI V2)......一切都按预期工作,但我非常不确定如何从 ClaimsPrincipal 检索值。我有...

回答 5 投票 0

ASP.NET Web API: JSON序列化循环引用

我正在通过ASP.NET Web API检索一个JSON对象图。我试图从一个子实体访问属性。然而,当查看浏览器的控制台时,它显示了一个引用($ref)到 ...

回答 1 投票 12

在C#中处理异常的Modbus 2.0

我正在使用web-api,其中我使用sontx的Modbus协议库。在文档中提到。异常处理 有几种情况下,响应被破坏,... ...

回答 1 投票 0

如何强制更新API中的IIS变化?

我有一个WebApp,其中有一些WebApi 2,WebApp是发布在服务器上运行的IIS。当我向WebApp添加新的内容时,更新工作正常,所有的内容都被提供......

回答 1 投票 1

强制某些代码总是在同一线程上运行。

我们有一个旧的第三方系统(我们称它为Junksoft®95),我们通过PowerShell与之接口(它暴露了一个COM对象),我正在将它包装在一个REST API中(ASP.NET框架4.8......)。

回答 3 投票 1

无法运行网络API

我有两个web-apis public string GetRData(string data) { var serialPort = new SerialPort("COM1", 9600, Parity.Even, 8, StopBits.One); try { if(serialPort......)

回答 1 投票 0

我如何在生成令牌时强制执行用户名和密码,或这是不好的做法。

我已经启动了一个简单的web api,并使用jwt为其添加了token生成功能。然而,我为用户商店使用了应用内账户,这是我设置token的功能。它显示在swagger......。

回答 1 投票 0

无法从web api中提取模型对象 OK() 响应。

我有一个模型类 public class Employee { public int Id { get; set; } public string Name { get; set; } public string Designation { get; set; } public string ...。

回答 1 投票 0

使用Directory.Delete()和Directory.CreateDirectory()来覆盖一个文件夹。

在我的WebApi动作方法中,我想用这段代码来创建一个文件夹: string myDir = "..."; if(Directory.Exists(myDir)) { Directory.Delete(myDir, true); }。Directory.CreateDirectory(...

回答 3 投票 12

CORS原点错误,使用Angular 8与web api2

我在Angular 8上使用web api 2工作。当我试图调用以下链接http:/localhost:20863apitoken登录时,它给我这个错误。在'http:/localhost:20863...'访问XMLHttpRequest。

回答 1 投票 0

在ASP.Net WebAPI中出现无法捕捉的InternalServerError。

我正在使用ASP.Net 4.7.2和ASP.Net Core 3.1 Webapplication作为客户端开发一个WebAPI。该WebAPI被托管在我的本地IIS上。我也在用一个windows窗体应用程序测试WebAPI。在...

回答 1 投票 0

Curl附加授权头,但控制器还是401。

我正在使用swagger gen ui,我正在使用以下设置,并按照GitHub的资源进行操作。根据GitHub,这似乎是swagger的一个已知问题,我使用的是一个基于jwt barrer ...

回答 1 投票 0

MVC5 Web API和依赖注入

试图在没有第三方工具的情况下,在Web API 2上做一些DI。所以,从一些例子来看,我已经有了自定义的依赖解析器(为什么没有集成的?奇怪,即使是Microsoft.Extensions...。

回答 1 投票 2

我是否需要将autofac的容器注册到自己的容器中,以连接webapi的depdendency resolver?

我正在使用Topshelf编写一个Windows服务,它应该启动一个自我托管的webapi项目和一个基于quickfixn的FIX服务。请考虑以下缩短的代码,到目前为止,它的工作。...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.