Get a complete url like http://google.com as action input

点点圈 提交于 2019-12-01 23:32:44

问题


I want to get url in my action to return the page's PageRank. I have a route like this:

routes.MapRoute(
    name: "PRURL",
    url: "Utility/PR/{*url}",
    defaults: new { controller = "Utility", action = "PR", url = UrlParameter.Optional }
);

but whenever I go to http://localhost:1619/Utility/PR/http://google.com I just get a System.Web.HttpException that tell me A potentially dangerous Request.Path value was detected from the client (:). I want to resolve this problem but I don't know how!

can anybody help me?


Update

I tried [ValidateInput(false)] but it doesn't resolved the problem! the action is this:

[ValidateInput(false)]
public string PR(string url)
{        
    return GooglePageRank.PRChecker.PR(url);
}

Update2 by adding

<system.web>
   <httpRuntime requestValidationMode="2.0" />
</system.web>

the problem didn't resolved! :(

finally the error resolved but the url changed from http://google.com to http:/google.com and I asked for it here and I got the answer.


回答1:


Try to add a requestPathInvalidCharacters attribute to httpRuntime

<httpRuntime requestValidationMode="2.0" 
    requestPathInvalidCharacters="&lt;,&gt;,*,%,&amp;,\,?" />

The default value is &lt;,&gt;,*,%,&amp;,:,\,?




回答2:


You can disable automatic request verification.

In web.config:

<pages validateRequest="false">

Or for action:

[ValidateInput(false)]
public ActionResult PR(...

And I forget about web.config. You need to add to web.config:

<system.web>
  <httpRuntime requestValidationMode="2.0" />
</system.web>

Or you can encode/decode url



来源:https://stackoverflow.com/questions/11838840/get-a-complete-url-like-http-google-com-as-action-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!