简体   繁体   English

使用参数从MVC控制器重定向到外部网址

[英]Redirect to external Url from MVC controller with parameters

i want to redirect the user to this link with being able to pass variables 我想通过传递变量将用户重定向到此链接

    public ActionResult GoogleMapAddress(string address, string Area, string city, string zipCode)
        {

           return Redirect(string.Format(" https://www.google.co.za/maps/search/{0}/ ", address + Area + city + zipCode));

        }

The View 风景

@Html.ActionLink("Address", "GoogleMapAddress", "Orders", new { address="test", Area="test", city="test", zipCode="test" },new  {target="_blank" })

The current method I have adds the Url link to the localhost link.Which gives the error- "A potentially dangerous Request.Path value was detected from the client (:)." 我当前使用的方法将Url链接添加到localhost链接。其中给出了错误“从客户端(:)检测到一个潜在危险的Request.Path值”。 and the url link(google) does work once I remove the added localhost link 一旦删除添加的localhost链接,URL链接(google)就会起作用

Here is the solution for the error,"A potentially dangerous Request.Path value was detected from the client (:)" 这是错误的解决方案,“从客户端(:)检测到潜在的危险Request.Path值”

Try these settings in webconfig file: 尝试在webconfig文件中进行以下设置:

<system.web>
    <httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0" />
    <pages validateRequest="false" />
</system.web>

Controller code: 控制器代码:

if you want to pass variable, give the value after the question mark like below 如果要传递变量,请在问号后给出值,如下所示

 public ActionResult GoogleMapAddress(string address, string Area, string city, string zipCode)
        {

           return Redirect(string.Format(" https://www.google.co.za/maps/search/{0}?inparam1="somevalue" ", address + Area + city + zipCode));

  }

As already mentioned in the comments the url needs to be properly constructed. 正如评论中已经提到的那样,需要适当地构建url。

first construct and encode the inserted segment. 首先构造并编码插入的片段。

var segment = string.Join(" ",address, Area, city, zipCode);
var escapedSegment = Uri.EscapeDataString(segment);

You then construct the complete URL with the base format and the escaped segment 然后,使用基本格式和转义的段来构造完整的URL。

var baseFormat = "https://www.google.co.za/maps/search/{0}/";
var url = string.Format(baseFormat, escapedSegment);

And use that to do the redirect. 并使用它来进行重定向。

Complete code would look something like this 完整的代码如下所示

public ActionResult GoogleMapAddress(string address, string Area, string city, string zipCode) {
    var segment = string.Join(" ",address, Area, city, zipCode);
    var escapedSegment = Uri.EscapeDataString(segment);
    var baseFormat = "https://www.google.co.za/maps/search/{0}/";
    var url = string.Format(baseFormat, escapedSegment);
    return Redirect(url);
}

You could even consider validating the constructed URL before trying to use it with if (Uri.IsWellFormedUriString(url, UriKind.Absolute)) 您甚至可以考虑在尝试将构造的URL与if (Uri.IsWellFormedUriString(url, UriKind.Absolute))一起使用之前,先对其进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM