中间件之后不会调用控制器操作

时间:2017-11-22 13:46:17

标签: c# asp.net asp.net-core

我一直试图让它发挥作用,但没有运气。

我有中间件类用于验证上传的文件。

但是在中间件之后,控制器动作没有触发。

如果有人可以帮助我或指出我的方向,那将非常感激。这几乎是我身边的午夜,我即将放弃。

这是中间件类:

public class UploadMiddleware
{
    private readonly RequestDelegate _next;

    private const uint FMFD_RETURNUPDATEDIMGMIMES = 0x20;
    private const uint RESERVED = 0;
    private const string UNKNOWN = "unknown/unknown";
    private const uint S_OK = 0;
    public UploadMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        var authToken = context.Request.Headers["id"].ToString();
        var path = context.Request.Path.ToString();

            if (authToken != String.Empty)
            {
                if (!IsMultipartContentType(context.Request.ContentType))
                {
                    await _next(context);
                    return;
                }

                var boundary = GetBoundary(context.Request.ContentType);
                var reader = new MultipartReader(boundary, context.Request.Body);
                var section = await reader.ReadNextSectionAsync();
                var buffer = new byte[256];
                while (section != null)
                {

                    var fileName = GetFileName(section.ContentDisposition);
                    var bytesRead = 0;
                    using (var stream = new FileStream(fileName,FileMode.Append))
                    {
                        do
                        {
                            bytesRead = await section.Body.ReadAsync(buffer, 0, buffer.Length);
                            stream.Write(buffer, 0, bytesRead);
                            buffer = Convert.FromBase64String(System.Text.Encoding.UTF8.GetString(buffer));

                        } while (bytesRead < 0);
                    }
                    if (!verifyUpcomingFiles(buffer))
                    {
                        await context.Response.WriteAsync("Undefined file type detected.");
                    }
                    // else
                    // {
                    //     await _next(context);

                    // }

                    section = await reader.ReadNextSectionAsync();
                }
                // await _next(context);
            }
            else
            {
                await context.Response.WriteAsync("You are not allowed to complete this process.");

            }

    }

Startup.cs类

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }


        app.UseStaticFiles();


            app.UseUploadMiddleware();
  app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");


        });
}

任何帮助将不胜感激,谢谢。

0 个答案:

没有答案