路径中的HTTP 404或非法字符使用HttpModule重写URL时出错 [英] HTTP 404 or Illegal characters in path Error while URL Rewriting using HttpModule

查看:162
本文介绍了路径中的HTTP 404或非法字符使用HttpModule重写URL时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



 HttpApplication MyApp =(HttpApplication)sender; 
string MyOldPath = MyApp.Request.Path;
switch (MyOldPath.ToLower())
{
case / profile
OriginalURL = 〜/ Modules / UserMgmt / Users.aspx?self = true;
break ;
默认
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,如下所示



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





如果我是使用任何url重写其url将如下所示



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







任何人都可以帮我解决这个问题。在此先感谢。

解决方案





最后我找到了解决方案。不是将查询字符串与原始路径一起写入,而是将其传递给context.Redirect方法的第三个参数。 ie)

 MyApp.context.Redirect(OriginalPath, string  .Empty,myQueryString)



带有三个参数的context.Redirect的语法是



 RewritePath(字符串字符串字符串



及其含义是使用给定的路径,路径信息和查询字符串信息重写URL。

参考: https://msdn.microsoft.com/en-us/library/system.web.httpcontext.rewritepath(v = VS 0.110)的.aspx


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

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.

I could see the current url when debugging as shown below

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



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

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




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

解决方案

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)

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


这篇关于路径中的HTTP 404或非法字符使用HttpModule重写URL时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆