Kow feng

Kow feng

  • NA
  • 2
  • 1.2k

How can I check all request url and return a custom object.

Sep 15 2018 5:09 AM
For example, when the URL request match (ex: .check) at the last of the URL, the server will return an object.

This should skip 404 error which means when I don't have the file in the server, it will still return an object no matter is an empty object.
 
Example:
www.localhost.com is my domain
 
1.  www.localhost.com/testing.check (do not have this file exists) should return a custom object
2.  www.localhost.com/testing/asdp.check (whether or not the path is exist, i just need the match of the last few words with .check) - return a custom object
Currently, im using Global.asax to check on Application_BeginRequest. However, it will hit 404 error when i only put domainname/{somethg}.check
 
Code as below:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string url = HttpContext.Current.Request.Url.AbsoluteUri;
Regex regex = new Regex(@".*\/.*\.check$");
Match match = regex.Match(url);
if (match.Success)
{
Response.Redirect("https://www.google.com");
}
else
{
Response.Redirect("https://www.youtube.com");
}
}
 
Wish someone can help with this. Thanks in advance. If the question is not clear, please feel free to ask for details. 

Answers (1)