Mr. Smith
Mr. Smith

Reputation: 4506

Can I return an HTTP 410 GONE and still serve the page content?

With ASP.NET, can I set Response.StatusCode to (int)HttpStatusCode.Gone and still serve the rest of the content of the page to the user? It seems the moment I set the StatusCode to a 410 the content of the page is removed and replaced with a generic message.

I know that ASP.NET allows me to change the style of the default error pages, but I don't think that's what I want in this case; I don't want the content of this page to be the default for all 410 errors on the website.

Upvotes: 4

Views: 2331

Answers (2)

David Negron
David Negron

Reputation: 1356

I've achieved this in the past by simply overriding the OnInit method of the Page and forcing it to return the desired HTTP Status Code. I've had to use this technique when creating custom error pages to insure that the proper HTTP Status Code is returned.

using System;
using System.Web.UI;

namespace Website
{
    public partial class _410 : Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Response.Buffer = true;
            Response.StatusCode = 410;
            Response.Status = "410 Gone";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

Upvotes: 3

usr
usr

Reputation: 171178

You can set TrySkipIisCustomErrors to avoid these custom errors.

Upvotes: 1

Related Questions