如何在 .Net Core API 中实现上传表单?

用户10863293

我创建了一个.Net Core API(我使用了swagger)。

我创建了一个控制器以上传图片以将其链接到项目。

.cs :

[HttpPut("[Action]/{id}")]
public async Task<ActionResult> Link(int id, IFormFile file)
{
    var item = await _context.Item.FirstOrDefaultAsync(t => t.Id == id);
    if (item == null)
    {
        return BadRequest("item null");
    }

    using (var memoryStream = new MemoryStream())
    {
        await file.CopyToAsync(memoryStream);
        // code to link
        return Ok(file);
    }
}

我的问题是,如果我想测试它是否有效,我必须使用邮递员,但我想在我的 api 中测试它。

我的问题有解决方案吗?目前它看起来像这样:上传文件

爱德华

对于Swashbuckle.AspNetCorewith4.0.1Swashbuckle.AspNetCore.Swaggerwith 4.0.1,它支持IFormFilewith swagger/index.html

详细步骤:

  1. 安装包Swashbuckle.AspNetCore4.0.1Swashbuckle.AspNetCore.Swagger4.0.1
  2. Startup.cs

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
    
        public IConfiguration Configuration { get; }
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
            });
    
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
    
            app.UseHttpsRedirection();
            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();
            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });
    
            app.UseMvc();
        }
    }
    
  3. ApiController

    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpPut("[Action]/{id}")]
        public async Task<ActionResult> Link(int id, IFormFile file)
        {
            return Ok(id);
        }
    }
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用WEB API Dot net core实现文件上传?

maxReceivedMessageSize 如何在 Asp.net Core Api 中设置?

如何在.net Core Web API中构造数据验证?

如何在Net Core Web API中覆盖OnAuthorization?

如何在.NET Core Web API中配置并发?

如何在 ASP.NET Core Web API 中实现搜索过滤器

.Net core web api - 如何在控制器中实现 PUT、POST 和 DELETE 方法?

如何在asp.net core mvc 6中实现模型绑定上传?

如何在asp.net core中访问从jquery文件上传发送的附加表单数据?

如何读取将从.Net Core API中的表单上载的文件?

如何使用其他表单控件(Angular 8)和asp.net core api上传图像

如何在ASP.Net Core中验证上传的文件

如何在ASP.NET Core Web API(无第三方)中实现JWT刷新令牌?

如何在同一进程中实现 ASP.NET Core Blazor Server 应用和 Web API 应用?

如何在 ASP .NET Core API 内部取消 API 请求?

如何在.net Core 2.2中实现JWT令牌

如何在ASP.NET Core 2.0中实现machineKey

如何在.net core 2.0中实现HttpContext?

如何在 ASP.NET Core 中实现设置页面?

如何在 ASP .NET Core 中实现基于 slug 的路由?

如何从 .NET Core api 中的 MVC 视图使用 API?

如何在 .NET Core API 中显示保存在数据库中的图像?

如何在 .NET Core 中调用存储在 appsettings.json 中的 API?

如何使用自定义 API 实现 .Net Core Identity

如何在ASP.NET Core 2中对Facebook Web API进行身份验证

如何在ASP.NET Core Web API中绑定Json查询字符串

如何在运行于ASP.NET Core 3.1的启用OData的Web API中添加Swagger

如何在中间件.Net Core Web API中获取当前的User / ClaimsPrincipal

如何在ASP.NET Core 2.1的API模型中获取用户IP地址?