Last post Feb 23, 2021 02:34 AM by CopBlaster
Feb 23, 2021 02:34 AM|CopBlaster|LINK
I have some Wordpress blogs used to syndicate content from some ASP.Net websites. Sometimes the content on the source sites gets removed, so I am working on a plugin that should allow users to delete syndicated posts if the URL of the source article returns
a 404 status code or a 301 status code if the location is set to a URL that all deleted posts are redirected to. To do this I need to request the headers using the PHP get_headers() function.
This approach works fine for most of my sites except for a couple of old sites that were built using ASP.Net 2.0 WebForms over a decade ago. Whenever I try to get the headers of any page on those sites I receive a 302 redirect to my custom error page saying
that the original URL I requested returned a 500 error code. What is it about older .Net websites that would cause every PHP header request to return a 500 error? I have tried getting the headers using various online header checking scripts with no problems.
I have other sites that use ASP.NEt 4.0+ WebForms and ASP.Net Core that do not have this problem. Only the older applications are at issue here. They also did not have that problem many years ago before I added a custom error redirect to the Web.Config file,
but I need a fix that ideally would not result in every server error resulting in a blank page with an error message.
The older sites have been upgraded to .Net Framework 4.7.2 and were recently converted to PWAs using a Workbox service worker. The caching from that service worker on some pages result in error redirects if a user tries to access a cached page that was cached
while they were logged in. This typically happens if the user logs out and Workbox tries to load a cached page from when they were logged in. This led to a dramatic increase in such errors, so I figured it would be easiest just to add a custom error page.
UPDATE: The custom error pages don't seem to be the cause. I tested an older site that has no custom error page and whenever I request headers from a page that normally returns a 300 status code I get a 500 despite the page being live on the web with no
problems in normal browsers. There is a difference however because when I feed a URL that has a valid 301 redirect it returns a valid 301 status code.
UPDATE: It seems that the problem is a function I use to set the MasterPage based on user agent. All of my web forms have the following code in them:
protected void Page_PreInit(object sender, System.EventArgs e)
var useragent = Request.ServerVariables["HTTP_USER_AGENT"];
this.MasterPageFile = setmasterpage.GetMaster(useragent);
That code calls this function:
public static string GetMaster(object agent)
string UserAgent = agent.ToString().ToLower();
string strMP = string.Empty;
if (UserAgent.Contains("phone") || UserAgent.Contains("android") || UserAgent.Contains("nexus") || UserAgent.Contains("mobile") || UserAgent.Contains("samsung"))
strMP = "~/Site.Mobile.Master";
strMP = "~/FinalMaster.Master";
Apparently PHP get_headers() does not include a user agent with the header request so the user agent is null which causes a null reference exception. I think my solution will need to be either finding a way to set a user agent when calling the PHP get_headers()
function or altering my GetMaster function to handle null user agent parameters.