c# - ASP.NET 5 / MVC 6 equivalent of HttpException -


in mvc 5 throw httpexception http code , set response so:

throw new httpexception((int)httpstatuscode.badrequest, "bad request."); 

httpexception not exist in asp.net 5 / mvc 6. equivalent code?

after brief chat @davidfowl, seems asp.net 5 has no such notion of httpexception or httpresponseexception "magically" turn response messages.

what can do, hook asp.net 5 pipeline via middleware, , create 1 handles exceptions you.

here example source code of error handler middleware set response status code 500 in case of exception further pipeline:

public class errorhandlermiddleware {     private readonly requestdelegate _next;     private readonly errorhandleroptions _options;     private readonly ilogger _logger;      public errorhandlermiddleware(requestdelegate next,                                    iloggerfactory loggerfactory,                                   errorhandleroptions options)     {         _next = next;         _options = options;         _logger = loggerfactory.createlogger<errorhandlermiddleware>();         if (_options.errorhandler == null)         {             _options.errorhandler = _next;         }     }      public async task invoke(httpcontext context)     {         try         {             await _next(context);         }         catch (exception ex)         {             _logger.logerror("an unhandled exception has occurred: " + ex.message, ex);              if (context.response.hasstarted)             {                 _logger.logwarning("the response has started,                                      error handler not executed.");                 throw;             }              pathstring originalpath = context.request.path;             if (_options.errorhandlingpath.hasvalue)             {                 context.request.path = _options.errorhandlingpath;             }             try             {                 var errorhandlerfeature = new errorhandlerfeature()                 {                     error = ex,                 };                 context.setfeature<ierrorhandlerfeature>(errorhandlerfeature);                 context.response.statuscode = 500;                 context.response.headers.clear();                  await _options.errorhandler(context);                 return;             }             catch (exception ex2)             {                 _logger.logerror("an exception thrown attempting                                   execute error handler.", ex2);             }                         {                 context.request.path = originalpath;             }              throw; // re-throw original if couldn't handle         }     } } 

and need register startup.cs:

public class startup {     public void configure(iapplicationbuilder app,                            ihostingenvironment env,                            iloggerfactory loggerfactory)     {        app.usemiddleware<exceptionhandlermiddleware>();     } } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -