【问题标题】:Access request path within endpoint routing MapFallbackToFile()端点路由 MapFallbackToFile() 中的访问请求路径
【发布时间】:2019-11-22 10:05:10
【问题描述】:

我正在创建一个启动 SPA 的 .NET Core 3.0 应用。 Startup.cs 中的路由将后备路由映射到文件 index.html。在提供index.html 文件之前,我想执行一些自定义验证,在此期间我需要访问原始请求路由。路由定义如下:

  app.UseEndpoints(endpoints =>
            {
                ......
                endpoints.MapFallbackToFile("index.html", new StaticFileOptions
                {
                    OnPrepareResponse = x =>
                    {
                       // access original request path here?
                    }
                });
            });

如何在OnPrepareResponse 方法中访问原始请求路径?

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    如何在 OnPrepareResponse 方法中访问原始请求路径?

    不知道为什么需要这样做。但是,StaticFileResponseContext 有一个 Context 属性,它引用了当前的 HttpContext,您可以通过它获取原始路径:

    endpoints.MapFallbackToFile("index.html", new StaticFileOptions
    {
        OnPrepareResponse = x =>
        {
            var httpContext = x.Context;
            var path = httpContext.Request.RouteValues["path"];
           // now you get the original request path
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 2016-03-08
      相关资源
      最近更新 更多