Asp.net Core:两个UseStatusCodePages扩展方法的差异仅在此吗?
Key Differences Between
UseStatusCodePagesWithRedirects and UseStatusCodePagesWithReExecute Great question! You’ve already nailed the core redirect behavior difference, but there are several other critical distinctions that impact your user experience, SEO, and how you handle error context. Let’s break them down:
1. HTTP Status Code Sent to Clients
UseStatusCodePagesWithRedirects: This sends a302 Foundredirect to the client, which triggers a new request to your error page. The final response the client receives is a200 OK(unless you explicitly set a different status on the error page). The original error status code (like 404 or 500) is not preserved for the client or search engines.UseStatusCodePagesWithReExecute: This does a server-side re-run of your error page handler, returning the original error status code directly to the client. For example, if the user hit a 404, they get a 404 status paired with your custom friendly page content—no client-side redirect occurs.
2. SEO Impact
UseStatusCodePagesWithRedirectsis bad for SEO: since the final status is 200, search engines will treat broken URLs as valid, failing to mark them as missing (404) or broken. This can hurt your site’s search ranking over time.UseStatusCodePagesWithReExecutesends the correct error status code, so search engines properly identify broken links or server errors, aligning with SEO best practices.
3. User Experience & URL Visibility
- With
UseStatusCodePagesWithRedirects, the user’s browser address bar will update to your error page URL (e.g., from/missing-pageto/Error?statusCode=404). This can feel disjointed if you want users to stay focused on the original URL they tried to access. UseStatusCodePagesWithReExecutekeeps the original URL in the address bar—users won’t see a redirect happen, leading to a smoother, more intuitive experience.
4. Access to Original Request Context
UseStatusCodePagesWithRedirectsrelies on client-side redirects, so passing details from the original request (like the path the user tried to access) requires query string parameters. This is limited and not ideal for detailed error reporting.UseStatusCodePagesWithReExecutelets you access the full original request context viaIStatusCodeReExecuteFeature. For example, you can pull the original path or status code directly in your error page handler:
This makes it easy to display targeted details like "The page {originalPath} couldn’t be found" or include support contact info tailored to the error type.var reExecuteFeature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>(); var originalPath = reExecuteFeature?.OriginalPath; var statusCode = HttpContext.Response.StatusCode;
Quick Example Snippets
Using UseStatusCodePagesWithRedirects
// Redirects client to /Error with status code in query string app.UseStatusCodePagesWithRedirects("/Error?statusCode={0}");
Using UseStatusCodePagesWithReExecute
// Server-side re-runs /Error handler, passing status code as query param app.UseStatusCodePagesWithReExecute("/Error", "?statusCode={0}");
Which Should You Choose?
If you want a user-friendly error page that preserves the original error status code, works well for SEO, and lets you access detailed request context, UseStatusCodePagesWithReExecute is the clear choice. Only use UseStatusCodePagesWithRedirects if you have a specific need for client-side redirects (like compatibility with older systems that don’t handle server-side re-execution well).
内容的提问来源于stack exchange,提问作者pk_code
相关产品推荐
相关产品推荐

