【问题标题】:ASP.NET Core: request returns 200 OK with no content: controller not instantiatedASP.NET Core:请求返回 200 OK,没有内容:控制器未实例化
【发布时间】:2018-07-20 15:14:58
【问题描述】:

我正在尝试运行 ASP.NET Core 服务器。我的服务器请求返回 200 OK,但没有返回任何内容,据我所知,控制器甚至没有被实例化。

Azure 流式日志的最后输入如下所示:

应用程序:2018-07-20 13:42:13.294 +00:00 [调试] Microsoft.AspNetCore.Routing.Tree.TreeRouter:请求成功匹配名称为'(null)'和模板'api / {的路由文化}/NotificationsReceived/{upToUtc}/{take}'。 应用程序:2018-07-20 13:42:13.670 +00:00 [调试] Microsoft.AspNetCore.Server.Kestrel:连接 ID“0HLFE8PANI4TL”完成保持活动响应。 应用程序:2018-07-20 13:42:13.670 +00:00 [信息] Microsoft.AspNetCore.Hosting.Internal.WebHost:请求在 413.5636ms 200 内完成

所以路由匹配并且请求完成,但我尝试将断点附加到控制器(远程调试器附加,但没有断点被命中,即使在我的中间件中也是如此),并从控制器记录信息如下:

[<Authorize>]
[<Route("api/{culture}/[controller]")>]
type NotificationsReceivedController(notifications: IReceivedNotifications, logger: ILog) =
    inherit Controller()
    do logger.Information "NotificationsReceivedController" "Created controller" None

    [<HttpGet("{upToUtc}/{take}")>]
    [<ProducesResponseType(200, Type = typeof<PageOfNotifications>)>]
    member this.Get(upToUtc:int64, take:int) =
        async {
            let upToUtc = new DateTime(upToUtc, DateTimeKind.Utc)
            logger.Information "NotificationsReceivedController" "GetReceivedNotifications endpoint" None
            let! notifications = notifications.GetAsync this.HttpContext upToUtc take
            return this.Ok(notifications) :> IActionResult
        } |> Async.StartAsTask

我不明白误报。如果我的管道出现问题,为什么没有在日志中找到它?如果没有任何问题,为什么路由匹配,但控制器没有被实例化?

【问题讨论】:

    标签: .net f# azure-web-app-service asp.net-core-2.0


    【解决方案1】:

    我设法使用异常过滤器解决了这个问题

    type ApiError = {
        ErrorType: Type
        Message: string
        StackTrace: string
    }
    
    type ApiExceptionAttribute(logger: ILog) =
        inherit ExceptionFilterAttribute()
        static let [<Literal>] Tag = "ApiExceptionAttribute"
        override __.OnException(context) =
            base.OnException context
            context.HttpContext.Response.StatusCode <-
                match context.Exception with
                | As (_: UnauthorizedAccessException) -> HttpStatusCode.Unauthorized |> int
                | _ -> HttpStatusCode.InternalServerError |> int
            let ex = context.Exception.GetBaseException()
            let apiError = {
                ErrorType = ex.GetType()
                Message = ex.Message
                StackTrace = ex.StackTrace
            }
            logger.Error Tag (apiError.ToString()) (Some ex)
            context.Result <- JsonResult apiError
    

    然后,按照标准做法,我将此过滤器设置为我的Startup.fs 文件中的全局过滤器:

    在我的 Startup.fs 文件中:

    services.AddMvc(fun config -> 
        config.RespectBrowserAcceptHeader <- true
        config.Filters.Add<ApiExceptionAttribute>() |> ignore
    ) |> ignore
    

    这解决了我的误报问题,让我能够追踪并修复代码中的错误。

    【讨论】:

      猜你喜欢
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 2020-11-03
      相关资源
      最近更新 更多