What I'm trying to accomplish:
Add a 301 redirect to the route collection to an external site. I think I'm not fully redirecting as I'm still getting the base domain in the route for some reason. For instance, if I'm redirecting to https://google.com and my site name is https://example.com. My redirection is: example.com/https://google.com. Before marking this as a duplicate please consider the "Other stack posts suggested" section at the bottom.
What error I'm getting:
A potentially dangerous Request.Path value was detected from the client (:).
Note: I don't believe this character is the problem because the base domain shouldn't be included in the url, and I have seen this thread.
What I'm trying:
On Application_Start()
in Global.aspx
I define a set of 301 redirects and add them to the RouteTable.Route
that property is just a RouteCollection
.
I add to those external routes to my RouteCollection like this:
public static void RegisterRoutes(RouteCollection routes)
{
...
//inside foreach, inside condition if the redirect is external, this hits put many conditional breakpoints here
routes.Add("Redirect" + redirect.redirect_id.ToString() + "Route",
new Route(redirect.old_path, new CustomRouteHandler("~/file/full-redirect.aspx")));
...
}
My CustomRouteHandler
class is defined as:
public class CustomRouteHandler : IRouteHandler
{
public CustomRouteHandler(string strVirtualPath)
{
this.VirtualPath = strVirtualPath;
}
public string VirtualPath { get; private set; }
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var page = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
return page;
}
}
My full-redirect.aspx.cs
redirect logic is defined as:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataRow dtRedirect = **query is here**;
//if you need extra query details I use Request.Path.ToLower().Substring(1) and where clause looking for the old path. This works in all other cases.
string newPath = dtRedirect["new_path"].ToString();
Response.Status = "301 Moved Permanently";
Response.AppendHeader("Location", newPath);
}
}
Other stack posts suggested:
- I've reviewed this thread
- Response.Redirect(newPath): I tried this and received the same result.
- Response.RedirectPermanent(newPath): I tried this and received the same result.
- Setting the Response.StatusCode to 301 along with appending the header.
- Setting Response.Redirect, StatusCode, and appending the header.
- Setting Response.RedirectPermanent, StatusCode, and appending the header.