Click here to Skip to main content
14,843,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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 "Illegal characters in path" Error.

Then I updated my webconfig as shown below.

xml
<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.

I could see the current url when debugging as shown below

vb
http://localhost:65305/Modules/UserMgmt/Users.aspx%3Fself=true


If I am not using any url rewrite its url will be as shown below

vb
http://localhost:65305/Modules/UserMgmt/Users.aspx?self=true



Can anyone help me to solve this issue. Thanks in advance.
Posted

1 solution

Hi,

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

c#
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
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900