Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the path in error handling #35596

Closed
gurkantuna opened this issue Aug 22, 2021 · 2 comments
Closed

Return the path in error handling #35596

gurkantuna opened this issue Aug 22, 2021 · 2 comments
Labels
area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.

Comments

@gurkantuna
Copy link

I handle the errors very well throughout the project as described in this section of the document. Personally, I prefer just one controller for errors.

Startup.cs

app.UseStatusCodePagesWithRedirects("/Error?status={0}");

ErrorController

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Index(int? status) {

    var errorViewModel = new ErrorViewModel {
        RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier
    };

    var exceptionHandlerPathFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
   //handle the error
}

It returns the HTTP status code with status variable , but I want to return the previous request path as well. I was writing a new middleware from the UseStatusCodePagesWithRedirects extension, but the controller cannot catch the error because I changed the original pattern to another one. eg.code={0}&path={1} . I try different features in HttpContext but they only return status code normally. To put it simply, I want to handle referrer uri to the error controller.

Feature Request

It should be one more query string named for example path or something else and it shuold returns the previous uri in the UseStatusCodePagesWithRedirects extension.

Should be

app.UseStatusCodePagesWithRedirects("/MyStatusCode?code={0}&path={1}");

In this way, the path can handle in the controller as follows.

public IActionResult Index(int? status, string path) {
@Tratcher
Copy link
Member

Note that UseStatusCodePages* handles more than error conditions, it would handle any response that did not provide a body. Responses without bodies are more common in Web API scenarios.

Have you tried using UseStatusCodePagesWithReExecute instead of UseStatusCodePagesWithRedirects? That behaves like UseExceptionhandler and preserves the request path in a feature as described here:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-5.0#usestatuscodepageswithreexecute

@Tratcher Tratcher added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Aug 23, 2021
@gurkantuna
Copy link
Author

@Tratcher Thanks for your suggestion. You are absolutely right, it was my fault 🙈 I had tried the 'UseStatusCodePagesWithRedirects' you pointed out before the post above, turns out I tried to handle it with type IExceptionHandlerPathFeature and the future was normally null. I looked the paramaters again after your suggestion and I replaced it with IStatusCodeReExecuteFeature which is the correct type. Then future is not null and the OriginalPath is returns the path.

Debug View

Thanks for your help 🍻

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Aug 24, 2021
@gurkantuna gurkantuna reopened this Aug 24, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Sep 23, 2021
@amcasey amcasey added area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware and removed area-runtime labels Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.
Projects
None yet
Development

No branches or pull requests

4 participants