question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Changing Httpcode status

See original GitHub issue

Hi, I want to have different HttpCodeStatus inside my AppService. For now all I can return are the HttpCode set by the AddFilters method that’s called inside the AddAbp. So only these filters

private static void AddFilters(MvcOptions options)
  {
                options.Filters.AddService(typeof(AbpAuthorizationFilter));
                options.Filters.AddService(typeof(AbpAuditActionFilter));
                options.Filters.AddService(typeof(AbpValidationActionFilter));
                options.Filters.AddService(typeof(AbpUowActionFilter));
                options.Filters.AddService(typeof(AbpExceptionFilter));
                options.Filters.AddService(typeof(AbpResultFilter));
  }

Here what im looking to modify is the AbpExceptionFilter. Because i want to be able to send other type of exception then those one define by the GetSatuscode of the abpExceptionFilter:

https://github.com/aspnetboilerplate/aspnetboilerplate/blob/96cf1e8c559105e940aaf24b35f3aa1abaa6c4fc/src/Abp.AspNetCore/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs

            protected virtual int GetStatusCode(ExceptionContext context)
            {
            if (context.Exception is AbpAuthorizationException)
            {
                        return context.HttpContext.User.Identity.IsAuthenticated
                        ? (int)HttpStatusCode.Forbidden
                        : (int)HttpStatusCode.Unauthorized;
            }
            if (context.Exception is AbpValidationException)
            {
                return (int)HttpStatusCode.BadRequest;
            }

            if (context.Exception is EntityNotFoundException)
            {
                return (int)HttpStatusCode.NotFound;
            }

            return (int)HttpStatusCode.InternalServerError;
        }
    }

How can i do so?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
acjhcommented, Apr 19, 2018

Set its order:

options.Filters.AddService(typeof(HttpGlobalExceptionFilter), order: 1);
1reaction
akinixcommented, Apr 17, 2018

try using the following code:

public class HttpGlobalExceptionFilter : AbpExceptionFilter
{
    public HttpGlobalExceptionFilter(IErrorInfoBuilder errorInfoBuilder, IAbpAspNetCoreConfiguration configuration)
        : base(errorInfoBuilder, configuration)
    {

    }

    protected override int GetStatusCode(ExceptionContext context)
    {
        // your code
        return base.GetStatusCode(context);
    }
}

add code into PreInitialize method of xxxModule, replace IExceptionFilter service,

Configuration.ReplaceService<IExceptionFilter, HttpGlobalExceptionFilter>(Abp.Dependency.DependencyLifeStyle.Transient);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can we change the status code of a HTTP request?
Can we change the status code of a HTTP request? · Enable SSL Proxying for testing domains · Right click URL → Select...
Read more >
Change the HTTP Status Code of a REST API
To set a different HTTP Status Code in the Response, do the following: Go to Manage Dependencies... and add the SetStatusCode action of...
Read more >
Is it acceptable to modify the text sent with the HTTP status ...
The status messages (technically called "reason phrases") are only recommendations and "MAY be changed without affecting the protocol).".
Read more >
Solved: Change and Set the HTTP status code Response
I want to set HTTP status code based on my backend service response. Though my backend service is failed some reason(Say Ex:Invalid Request)...
Read more >
HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found