HTTP 404 or Illegal Characters in Path Error while rewriting urls using HttpModule
I would like to rewrite all urls in my project. I am doing this using HttpModule. It works fine, no query string. But some pages require a query string. So he cannot avoid it. This is how I wrote my HttpModule.
HttpApplication MyApp = (HttpApplication)sender;
string MyOldPath = MyApp.Request.Path;
switch (MyOldPath.ToLower())
{
case "/profile":
OriginalURL = "~/Modules/UserMgmt/Users.aspx?self=true";
break;
default:
break;
}
if (OriginalURL != string.Empty)
MyApp.Context.RewritePath(OriginalURL, string.Empty, string.Empty);
When I do this, I got the error "Illegal characters in the path"
Then I updated my webConfig as shown below.
<system.web>
</httpModules>
<httpRuntime requestValidationMode="2.0" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters="<,>,*,%,:,&,\" />
</system.web>
At that time, the error "Invalid characters in the path" was resolved and received an Http 404 error.
I could see the current url when debugging like below
If I am not using the url then rewrite its url as shown below
Could you please help me to solve this problem. Thanks in advance.
source to share
Here's the solution .... Instead of writing the query string along with the original path, pass it to the third parameter, context.Redirect. i.e.) MyApp.context.Redirect(OriginalPath,string.Empty,myQueryString)
. Context.Redirect syntax with three parameters
RewritePath(String, String, String)
and its value Overrides the URL with the given path, path information, and query string information . Link: https://msdn.microsoft.com/en-us/library/system.web.httpcontext.rewritepath(v=vs.110).aspx
source to share