We are trying to implement a non-hosted header that will accept anything before *.website.com
in ASP.NET. Since it will accept any subdomain, we extended the HttpContextBase class to add custom method.
public static bool ValidateHost(this HttpContextBase context)
{
var domain = context.Request.Url.Host;
//add logic to check if the host is valid and the subdomain exist in the database
return false;
}
This method will validate whether the context.Url.Host
is a valid host or its subdomain exist in the database if not then redirect the request to the default host website.com
. To do that I added this line of codes below in our BaseController
:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.HttpContext.ValidateUrl())
{
filterContext.HttpContext.Response.Redirect("https://website.com/");
return;
}
}
It redirects to default host whenever it returns false
, however it throws an exception: {"Server cannot append header after HTTP headers have been sent."}
Am I missing something here or the logic is incomplete?