隐藏

路径中的HTTP 404或非法字符使用HttpModule重写URL时出错(HTTP 404 or Illegal characters in path Error while URL Rewriti

发布:2021/7/15 11:35:32作者:管理员 来源:本站 浏览次数:1019

我想重写我项目中的所有网址。 我是在HttpModule的帮助下完成的。 它工作正常,没有任何查询字符串。 但在某些页面中查询字符串是必不可少的 所以无法避免它。 这就是我编写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); 

当我继续这个时,我收到错误“路径中的非法字符”

然后我更新了我的webConfig,如下所示

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

那时“路径中的非法字符”错误解决了,并得到了Http 404错误。

在此处输入图像描述

调试时我可以看到当前的url,如下所示 在此处输入图像描述

如果我没有使用任何url重写其url,如下所示 在此处输入图像描述

你能帮我解决这个问题吗? 提前致谢。


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.