asp.net核心路由特定的中间件

时间:2018-08-21 19:55:08

标签: c# asp.net-core

我正在使用KOA 2.0,并开始测试asp.net核心。但是找不到处理请求/ URL特定中间件的方法

在Koa中说,借助路由器,我可以实现以下目标:

.post("/api/v1/user", Middleware1, Middleware2, RequestValidationMiddleware, SpecificAction);
.get("/api/v1/user", Middleware1, Middleware2, RequestValidationMiddleware, SpecificAction1);
.post("/api/v1/role", Middleware1, Middleware4, RequestValidationMiddleware2, SpecificAction2);

如何使用asp.net核心实现它?

尝试了以下内容:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    //app.UseApiLog();
    app.Map("/api", ApiLogApps);
    app.Map("/exlog", ExceptionLogApps);

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

private static void ApiLogApps(IApplicationBuilder app)
{
    //app.Run(() => )
    app.UseApiLog();
    app.UseMvc();
}

在控制器中,我有

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    [HttpGet("test/get/{id}")]
    public string Get(int id)
    {
        return "value";
    }
}

但是我在这里迷路了。

我想要的是,我想让DataValidation在中间件中进行处理-迫使我必须针对每个url(几乎)使用特定的中间件。

PS-我知道,模型验证可以在实际中完成,但我不希望这样做。

在此先感谢您的帮助:)

2 个答案:

答案 0 :(得分:4)

要使用Koa2之类的中间件,您可以配置一系列中间件来构建路由:

    public IRouter BuildRouter(IApplicationBuilder applicationBuilder)
    {
        var builder = new RouteBuilder(applicationBuilder);

        // use middlewares to configure a route
        builder.MapMiddlewareGet("/api/v1/user", appBuilder => {
            // your Middleware1
            appBuilder.Use(Middleware1);
            appBuilder.Use(Middleware2);
            appBuilder.Use(RequestValidationMiddleware);
            appBuilder.Run(SpecificAction);
        });

        builder.MapMiddlewarePost("/api/v1/user", appBuilder => {
            // your Middleware1
            appBuilder.Use(Middleware1);
            appBuilder.Use(Middleware2);
            appBuilder.Use(RequestValidationMiddleware);
            appBuilder.Run(SpecificAction1);
        });
        // ....

        return builder.Build();
    }

,然后通过UseRouter(router)使用RouterMiddleware:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // ...
    app.UseRouter(BuildRouter(app));
    // ...
    app.UseMvc();
}

屏幕截图:

enter image description here

[更新]:

要与属性路由集成,只需添加UseMvc()的{​​{1}}指令,如下所示:

Run()

就演示而言,Middleware1是一个虚拟的中间件,它添加了 public IRouter BuildRouter(IApplicationBuilder applicationBuilder) { var builder = new RouteBuilder(applicationBuilder); // use middlewares to configure a route builder.MapMiddlewareGet("/api/v1/user", appBuilder => { appBuilder.Use(Middleware1); appBuilder.Use(Middleware2); appBuilder.Use(RequestValidationMiddleware); appBuilder.UseMvc(); // use a MVC here ... }); builder.MapMiddlewarePost("/api/v1/user", appBuilder => { appBuilder.Use(Middleware1); appBuilder.Use(Middleware2); appBuilder.Use(RequestValidationMiddleware); appBuilder.UseMvc(); }); // .... return builder.Build(); }

HttpContext.Items['mw-message1']

控制器是一个普通的控制器,它将检索 private Func<RequestDelegate, RequestDelegate> Middleware1 = next=> { return async context => { context.Items["mw-message1"] = "mw1"; await next(context); }; };

HttpContext.Items["mw-messages1"]

现在,当向[Route("api/v1/[controller]")] [ApiController] public class UserController : ControllerBase { public IActionResult Index() { var x = (string)HttpContext.Items["mw-message1"]; return new JsonResult(new { MW1 = x, Hello= "Hello", }); } } 发出Get请求时,最终响应为:

/api/v1/user

答案 1 :(得分:0)

我在 .net core 3 webapi 中使用以下代码。

在startup.cs配置方法下面添加,为特定路由添加中间件

//用于检查请求头中是否存在授权密钥的中间件 //映射到所有路由

_ = app.UseHeaderKeyAuthorization();

//映射到所有路由

_ = app.MapWhen(context => context.Request.Path.StartsWithSegments("/api/v1.0"), appBuilder =>
{
    _ = appBuilder.UseHeaderKeyAuthorization();
});

创建中间件HeaderKeyAuthorizationMiddleware,示例如下

//HeaderKeyAuthorizationMiddleware
public class HeaderKeyAuthorizationMiddleware
    {
        private readonly RequestDelegate next;

        public HeaderKeyAuthorizationMiddleware(RequestDelegate next)
        {
            this.next = next;
        }

        public async Task Invoke(HttpContext httpContext)
        {
            var authHeader = httpContext.Request.Headers[ApiConstants.AuthHeaderName];
            //check authHeader logic
            if (!string.IsNullOrEmpty(authHeader))
            {
                await next.Invoke(httpContext);
                return;
            }

            //Reject request if there is no authorization header or if it is not valid
            httpContext.Response.StatusCode = 401;
            await httpContext.Response.WriteAsync("Unauthorized");
        }
    }

//Middleware extension to register middleware   
public static class HeaderKeyAuthorizationMiddlewareExtension
    {
        public static IApplicationBuilder UseHeaderKeyAuthorization(this IApplicationBuilder app)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            return app.UseMiddleware<HeaderKeyAuthorizationMiddleware>();
        }
    }