|
@@ -6,8 +6,10 @@ using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
using System.Net.Http;
|
|
|
+using System.Runtime.Remoting.Contexts;
|
|
|
using System.Text;
|
|
|
using System.Web.Http;
|
|
|
+using System.Web.Http.Description;
|
|
|
using System.Web.Http.Filters;
|
|
|
using System.Web.Http.Metadata;
|
|
|
using System.Web.Http.Routing;
|
|
@@ -86,6 +88,7 @@ namespace XdCxRhDW.App.WebAPI
|
|
|
c.UseFullTypeNameInSchemaIds();
|
|
|
//加入控制器描述
|
|
|
c.CustomProvider((defaultProvider) => new SwaggerControllerDescProvider(defaultProvider, webApiXmlPath1));
|
|
|
+ c.OperationFilter<FileUploadOperation>();
|
|
|
})
|
|
|
.EnableSwaggerUi(c =>
|
|
|
{
|
|
@@ -93,7 +96,84 @@ namespace XdCxRhDW.App.WebAPI
|
|
|
c.DocumentTitle("");
|
|
|
});
|
|
|
}
|
|
|
+ /// <summary>
|
|
|
+ /// Swagger文件上传特性标注
|
|
|
+ /// </summary>
|
|
|
+ [AttributeUsage(AttributeTargets.Method)]
|
|
|
+ public sealed class SwaggerFormAttribute : Attribute
|
|
|
+ {
|
|
|
+ public SwaggerFormAttribute()
|
|
|
+ {
|
|
|
+ this.Name = "文件";
|
|
|
+ this.Description = "选择文件";
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// Swagger特性标注
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="name"></param>
|
|
|
+ /// <param name="description"></param>
|
|
|
+ public SwaggerFormAttribute(string name, string description)
|
|
|
+ {
|
|
|
+ Name = name;
|
|
|
+ Description = description;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 名称
|
|
|
+ /// </summary>
|
|
|
+ public string Name { get; private set; }
|
|
|
+ /// <summary>
|
|
|
+ /// 描述
|
|
|
+ /// </summary>
|
|
|
+ public string Description { get; private set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class FileUploadOperation : IOperationFilter
|
|
|
+ {
|
|
|
+ public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
|
|
|
+ {
|
|
|
+ if (operation.parameters == null)
|
|
|
+ {
|
|
|
+ operation.parameters = new List<Parameter>();
|
|
|
+ }
|
|
|
+ var requestAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerFormAttribute>();
|
|
|
+ foreach (var attr in requestAttributes)
|
|
|
+ {
|
|
|
+ operation.parameters.Add(new Parameter
|
|
|
+ {
|
|
|
+ description = attr.Description,
|
|
|
+ name = attr.Name,
|
|
|
+ @in = "formData",
|
|
|
+ required = true,
|
|
|
+ type = "file",
|
|
|
+ });
|
|
|
+ operation.consumes.Add("multipart/form-data");
|
|
|
+ }
|
|
|
+ //if (operation.operationId.ToLower() == "apivaluesuploadpost")
|
|
|
+ //{
|
|
|
+ // operation.parameters.Clear();
|
|
|
+ // operation.parameters.Add(new Parameter
|
|
|
+ // {
|
|
|
+ // name = "uploadedFile",
|
|
|
+ // @in = "formData",
|
|
|
+ // description = "Upload File",
|
|
|
+ // required = true,
|
|
|
+ // type = "file"
|
|
|
+ // });
|
|
|
+ // operation.consumes.Add("multipart/form-data");
|
|
|
+ //}
|
|
|
|
|
|
+ //判断上传文件的类型,只有上传的类型是IFormCollection的才进行重写。
|
|
|
+ //var paras = apiDescription.ActionDescriptor.GetParameters();
|
|
|
+ //if (paras.Any(w => w.ParameterType == typeof(IFormCollection)))
|
|
|
+ //{
|
|
|
+ // Dictionary<string, OpenApiSchema> schema = new Dictionary<string, OpenApiSchema>();
|
|
|
+ // schema["fileName"] = new OpenApiSchema { Description = "Select file", Type = "string", Format = "binary" };
|
|
|
+ // Dictionary<string, OpenApiMediaType> content = new Dictionary<string, OpenApiMediaType>();
|
|
|
+ // content["multipart/form-data"] = new OpenApiMediaType { Schema = new OpenApiSchema { Type = "object", Properties = schema } };
|
|
|
+ // operation.RequestBody = new OpenApiRequestBody() { Content = content };
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ }
|
|
|
public class HandlerErrorAttribute : ExceptionFilterAttribute
|
|
|
{
|
|
|
/// <summary>
|