locked
asp net 4.61 WebAPI - Error catching RRS feed

  • Question

  • User-1668014665 posted

    TESTING in POSTMAN - Chrome App

    The URL below works fine, and errors around this URL design I can catch

    http://localhost:49327/api/data?Symbol=AAPL

    Question1 how can I tidy up the ugly error reports when a User makes a mistake like this
    http://localhost:49327/api/

    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>IIS 10.0 Detailed Error - 404.0 - Not Found</title>
            <style type="text/css">
                <!-- 
    body{margin:0;font-size:.7em;font-family:Verdana,Arial,Helvetica,sans-serif;} 
    code{margin:0;color:#006600;font-size:1.1em;font-weight:bold;} 
    .config_source code{font-size:.8em;color:#000000;} 
    
    Etc
    etc

    Gives a 404, do I create a webconfig custom error HTML page or something??

    Question2 How does catch this error, a variable is Not entered on the URL
    http://localhost:49327/api/data?symbol= 

    After Symbol=  there is nothing, 

    in Code Symbol is a string

    Checking for Symbol ="" in code does not catch this error, can there be a NULL Check or something else

    Thanks

    Monday, March 4, 2019 10:51 PM

Answers

  • User1120430333 posted

    I use a filter to catch all unhandled exceptions, which means there are no try/catch in the WebAPI or a classlib project code the WebAPI is referencing, and it's all caught by the global exception handler.

    https://stackify.com/csharp-catch-all-exceptions/

    The exception was caught and logged by that WebAPI, but the ASP.NET MVC project the presentation layer  that used the WebAPI also had the exception returned. The MVC project knew about the exception that was thrown by the WebAPI,  and it too threw an exemption so that its GEH logged the error and notified the user at the screen that something bad had happened. 

    Imports System.Net
    Imports System.Net.Http
    Imports System.Web.Http.Filters
    Imports log4net
    
    Public Class CustomExceptionFilter
        Inherits ExceptionFilterAttribute
    
        private ReadOnly _logger As ILog
    
        public sub New()
            _logger = LogManager.GetLogger(GetType(CustomExceptionFilter))
        End sub
    
        Public Overrides Sub OnException(actionExecutedContext As HttpActionExecutedContext)
            MyBase.OnException(actionExecutedContext)
    
            Dim exceptionMessage as String = String.Empty
    
            If IsNothing(actionExecutedContext.Exception.InnerException ) Then
                exceptionMessage = actionExecutedContext.Exception.Message _
                                   & " " & actionExecutedContext.Exception.StackTrace
            Else 
                exceptionMessage =  actionExecutedContext.Exception.Message _
                                    & " " & actionExecutedContext.Exception.InnerException.Message _
                                    & " " & actionExecutedContext.Exception.StackTrace
            End If
    
            _logger.Error(exceptionMessage)
    
            dim response =  New HttpResponseMessage(HttpStatusCode.InternalServerError)With {.Content = New StringContent(“An unhandled exception was thrown by service.”),
                    .ReasonPhrase = "Internal Server Error.Please Contact your Administrator."}
    
            actionExecutedContext.Response = response
    
        End Sub
    End Class
    
    Imports System.Web.Http
    Imports DAL
    Imports Entities
    
    Namespace Controllers
    
        <CustomExceptionFilter>
        Public Class ProjectController
            Inherits ApiController
    
            Private ReadOnly _daoproject As IDaoProject
    
            public sub New (daoproject As IDaoProject)
                _daoproject = daoproject
            End sub
    
            <HttpGet>
            <ActionName("GetProjectById")>
            public Function GetProjectById(ByVal id As Int32) As DtoProject
                return _daoproject.GetProjectById(id)
            End Function
    
    
            <HttpGet>
            <ActionName("GetProjectsByUserId")>
            public Function GetProjectsByUserId(ByVal userid As String) As List(Of DtoProject)
                return _daoproject.GetProjectsByUserId(userid)
            End Function
    
            <HttpPost>
            <ActionName("CreateProject")>
            public sub CreateProject(ByVal dto As DtoProject)
                Call _daoproject.CreateProject(dto)
            End sub
            
            <HttpPost>
            <ActionName("UpdateProject")>
            public sub UpdateProject(ByVal dto As DtoProject)
                Call _daoproject.UpdateProject(dto)
            End sub
    
            <HttpPost>
            <ActionName("DeleteProject")>
            public sub  DeleteProject(ByVal dto As DtoId)
                Call _daoproject.DeleteProject(dto.Id)
            End sub
            
        End Class
    End Namespace

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 5, 2019 12:16 AM

All replies

  • User475983607 posted

    TESTING in POSTMAN - Chrome App

    The URL below works fine, and errors around this URL design I can catch

    http://localhost:49327/api/data?Symbol=AAPL

    Question1 how can I tidy up the ugly error reports when a User makes a mistake like this
    http://localhost:49327/api/

    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>IIS 10.0 Detailed Error - 404.0 - Not Found</title>
            <style type="text/css">
                <!-- 
    body{margin:0;font-size:.7em;font-family:Verdana,Arial,Helvetica,sans-serif;} 
    code{margin:0;color:#006600;font-size:1.1em;font-weight:bold;} 
    .config_source code{font-size:.8em;color:#000000;} 
    
    Etc
    etc

    Gives a 404, do I create a webconfig custom error HTML page or something??

    Question2 How does catch this error, a variable is Not entered on the URL
    http://localhost:49327/api/data?symbol= 

    After Symbol=  there is nothing, 

    in Code Symbol is a string

    Checking for Symbol ="" in code does not catch this error, can there be a NULL Check or something else

    Thanks

    The docs cover the basics.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handling

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/web-api-global-error-handling

    But yes, you can use filters to tidy up general errors.

    Monday, March 4, 2019 11:51 PM
  • User1120430333 posted

    I use a filter to catch all unhandled exceptions, which means there are no try/catch in the WebAPI or a classlib project code the WebAPI is referencing, and it's all caught by the global exception handler.

    https://stackify.com/csharp-catch-all-exceptions/

    The exception was caught and logged by that WebAPI, but the ASP.NET MVC project the presentation layer  that used the WebAPI also had the exception returned. The MVC project knew about the exception that was thrown by the WebAPI,  and it too threw an exemption so that its GEH logged the error and notified the user at the screen that something bad had happened. 

    Imports System.Net
    Imports System.Net.Http
    Imports System.Web.Http.Filters
    Imports log4net
    
    Public Class CustomExceptionFilter
        Inherits ExceptionFilterAttribute
    
        private ReadOnly _logger As ILog
    
        public sub New()
            _logger = LogManager.GetLogger(GetType(CustomExceptionFilter))
        End sub
    
        Public Overrides Sub OnException(actionExecutedContext As HttpActionExecutedContext)
            MyBase.OnException(actionExecutedContext)
    
            Dim exceptionMessage as String = String.Empty
    
            If IsNothing(actionExecutedContext.Exception.InnerException ) Then
                exceptionMessage = actionExecutedContext.Exception.Message _
                                   & " " & actionExecutedContext.Exception.StackTrace
            Else 
                exceptionMessage =  actionExecutedContext.Exception.Message _
                                    & " " & actionExecutedContext.Exception.InnerException.Message _
                                    & " " & actionExecutedContext.Exception.StackTrace
            End If
    
            _logger.Error(exceptionMessage)
    
            dim response =  New HttpResponseMessage(HttpStatusCode.InternalServerError)With {.Content = New StringContent(“An unhandled exception was thrown by service.”),
                    .ReasonPhrase = "Internal Server Error.Please Contact your Administrator."}
    
            actionExecutedContext.Response = response
    
        End Sub
    End Class
    
    Imports System.Web.Http
    Imports DAL
    Imports Entities
    
    Namespace Controllers
    
        <CustomExceptionFilter>
        Public Class ProjectController
            Inherits ApiController
    
            Private ReadOnly _daoproject As IDaoProject
    
            public sub New (daoproject As IDaoProject)
                _daoproject = daoproject
            End sub
    
            <HttpGet>
            <ActionName("GetProjectById")>
            public Function GetProjectById(ByVal id As Int32) As DtoProject
                return _daoproject.GetProjectById(id)
            End Function
    
    
            <HttpGet>
            <ActionName("GetProjectsByUserId")>
            public Function GetProjectsByUserId(ByVal userid As String) As List(Of DtoProject)
                return _daoproject.GetProjectsByUserId(userid)
            End Function
    
            <HttpPost>
            <ActionName("CreateProject")>
            public sub CreateProject(ByVal dto As DtoProject)
                Call _daoproject.CreateProject(dto)
            End sub
            
            <HttpPost>
            <ActionName("UpdateProject")>
            public sub UpdateProject(ByVal dto As DtoProject)
                Call _daoproject.UpdateProject(dto)
            End sub
    
            <HttpPost>
            <ActionName("DeleteProject")>
            public sub  DeleteProject(ByVal dto As DtoId)
                Call _daoproject.DeleteProject(dto.Id)
            End sub
            
        End Class
    End Namespace

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 5, 2019 12:16 AM