Programatically ask a page, in global asax if its marked obsolete?
I would like to mark some pages in my site that is currently in the wild as "obsolete" and pick up on that in my global.asax and redirect the client to a different page.
Then I wil开发者_开发问答l have the site notify the dev team that someone tried to hit that page.
Can you access the Type information for the page from any global place?
Provided that you have the following page:
[Obsolete]
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
}
In global.asax you could do this:
protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
if (!(this.Context.Handler is System.Web.UI.Page))
{
return;
}
var isPageObsolete = this.Context.Handler
.GetType()
.BaseType
.GetCustomAttributes(typeof(ObsoleteAttribute), true)
.Length > 0;
if (isPageObsolete)
{
Response.Redirect("http://www.google.com");
}
}
I think Darin's answer is quite creative. I'm offering another perspective. The method discussed so far is marking the page AT the page. How about marking the page say in the config or another xml file like this.
<obsoletePages>
<page path="/page123.aspx />
<page path="/pagexyz.aspx />
</obsoletePages>
At global.asax, intercept the request at Application_BeginRequest(), if the request page is found within the configuration file, perform the redirect.
精彩评论