Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP 404 or Illegal characters in path Error while URL Rewriting using HttpModule

Tags:

c#

asp.net

I would like to rewrite all the urls in my project. I am doing it with the help of HttpModule. Its working fine without any query string. But in some pages query string is essential. So can't avoid it. This is the way I've written 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 proceed this I got the error " Illegal characters in path"

Then I updated my webConfig as shown below

    <system.web>
        </httpModules>
        <httpRuntime requestValidationMode="2.0" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters="&lt;,&gt;,*,%,:,&amp;,\" />
  </system.web>

At that time the " Illegal characters in path" error solved and getting the Http 404 Error.

enter image description here

I could see the current url when debugging as shown below enter image description here

If I am not using any url rewrite its url as shown belowenter image description here

Could you help me to solve this issue. Thanks in advance.

like image 460
Libin C Jacob Avatar asked Apr 17 '26 01:04

Libin C Jacob


1 Answers

Here is the solution....Instead of writing the query string along with the original path, passes it to the third parameter of context.Redirect method. ie) MyApp.context.Redirect(OriginalPath,string.Empty,myQueryString). The syntax of context.Redirect with three parameter is

RewritePath(String, String, String)

and its meaning is Rewrites the URL by using the given path, path information, and query string information. Reference: https://msdn.microsoft.com/en-us/library/system.web.httpcontext.rewritepath(v=vs.110).aspx

like image 139
SRJ Avatar answered Apr 18 '26 13:04

SRJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!