在C#中实现swagger的问题

时间:2018-02-12 07:10:06

标签: c# rest swagger

我创建了以下网络服务:

    /// <summary>
    /// Get all users from CRS that are not included in the emaillist
    /// </summary>
    /// <param name="emailList">string with emails separated by semicolon</param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/user/getwurusersnotinlist/")]
    [SwaggerOperation("UserGetWurUsersNotInList")]
    public List<UserDto> GetWurUsersNotInList([FromBody] string emailList)
    {
        var list = Request.Content.ReadAsStringAsync().Result;

        var exclude = (emailList??string.Empty).ToLower().Split(';').ToList();

        return WurUsers
            .Where(user => string.IsNullOrEmpty(user.Email) && !exclude.Contains(user.Email.ToLower()))
            .Select(user => user.Dto())
            .ToList();
    }

正如您所看到的,我正在尝试使用[FromBody] Swagger属性。在客户端,以下代码用于检索用户数据:

        StringBuilder sb = new StringBuilder();
        var semi = string.Empty;

        foreach (var user in students)
        {
            sb.Append(semi).Append(user.EMail);
            semi = ";";
        }

        var excludeList = sb.ToString();

        var crsUsers = UserServiceClient.UserGetWurUsersNotInList(excludeList);

        foreach (UserDto crsUser in crsUsers)
        {
            ...
        }

此代码的问题是:

  1. emailList始终为null
  2. 从Request.Content读取的列表包含列表减去前4个字符。很奇怪。
  3. 当结果返回时,我得到一个异常,说'无法反序列化响应'。内部:解析值时遇到意外的字符:&lt;。路径'',第0行,第0位。 看起来代码正在发送一个需要JSON的XML。
  4. ===编辑===

    以下是客户端Swagger生成的代码:

        // Code generated by Microsoft (R) AutoRest Code Generator 0.16.0.0
    // Changes may cause incorrect behavior and will be lost if the code is
    // regenerated.
    
    namespace WUR.BBCourseEnrollment.Business
    {
        using System;
        using System.Collections.Generic;
        using System.Net.Http;
        using System.Threading;
        using System.Threading.Tasks;
        using Newtonsoft.Json;
        using Microsoft.Rest;
        using Models;
    
        /// <summary>
        /// </summary>
        public partial interface IWurCrsWeb : IDisposable
        {
            /// <summary>
            /// The base URI of the service.
            /// </summary>
            Uri BaseUri { get; set; }
    
            /// <summary>
            /// Gets or sets json serialization settings.
            /// </summary>
            JsonSerializerSettings SerializationSettings { get; }
    
            /// <summary>
            /// Gets or sets json deserialization settings.
            /// </summary>
            JsonSerializerSettings DeserializationSettings { get; }
    
            /// <summary>
            /// Subscription credentials which uniquely identify client
            /// subscription.
            /// </summary>
            ServiceClientCredentials Credentials { get; }
    
    
                /// <param name='customHeaders'>
            /// The headers that will be added to request.
            /// </param>
            /// <param name='cancellationToken'>
            /// The cancellation token.
            /// </param>
            Task<HttpOperationResponse<IList<UserDto>>> UserGetAllWithHttpMessagesAsync(Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
    
            /// <param name='emailList'>
            /// </param>
            /// <param name='customHeaders'>
            /// The headers that will be added to request.
            /// </param>
            /// <param name='cancellationToken'>
            /// The cancellation token.
            /// </param>
            Task<HttpOperationResponse<IList<UserDto>>> UserGetWurUsersNotInListWithHttpMessagesAsync(string emailList, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
    
            /// <param name='customHeaders'>
            /// The headers that will be added to request.
            /// </param>
            /// <param name='cancellationToken'>
            /// The cancellation token.
            /// </param>
            Task<HttpOperationResponse<IList<ScheduleDto>>> UserGetSchedulesWithBlackboardIdWithHttpMessagesAsync(Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
    
        }
    }
    

0 个答案:

没有答案