C# 使用参数从MVC控制器重定向到外部Url

C# 使用参数从MVC控制器重定向到外部Url,c#,asp.net-mvc,url,C#,Asp.net Mvc,Url,我想通过传递变量将用户重定向到此链接 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))

我想通过传递变量将用户重定向到此链接

    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));

        }
景色

@Html.ActionLink("Address", "GoogleMapAddress", "Orders", new { address="test", Area="test", city="test", zipCode="test" },new  {target="_blank" })
我使用的当前方法将Url链接添加到localhost链接。这会导致错误- “潜在危险的请求。从客户端(:)检测到路径值。”
一旦我删除添加的本地主机链接,url链接(google)就可以工作了。下面是错误的解决方案,“从客户端检测到一个潜在危险的请求。路径值(:)”

在webconfig文件中尝试以下设置:

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

以下是错误的解决方案,“从客户端(:)检测到一个潜在危险的请求。路径值”

在webconfig文件中尝试以下设置:

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

正如评论中已经提到的,url需要正确构造

首先构造并编码插入的段

var segment = string.Join(" ",address, Area, city, zipCode);
var escapedSegment = Uri.EscapeDataString(segment);
然后用基本格式和转义段构造完整的URL

var baseFormat = "https://www.google.co.za/maps/search/{0}/";
var url = string.Format(baseFormat, escapedSegment);
然后用它来重定向

完整的代码如下所示

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);
}

您甚至可以考虑在使用“代码”之前验证构建的URL。如果(URI IsWellFormedUriString(URL,UriKind。绝对))>/P>< P>如已在评论中提到的,URL需要正确构建。

首先构造并编码插入的段

var segment = string.Join(" ",address, Area, city, zipCode);
var escapedSegment = Uri.EscapeDataString(segment);
然后用基本格式和转义段构造完整的URL

var baseFormat = "https://www.google.co.za/maps/search/{0}/";
var url = string.Format(baseFormat, escapedSegment);
然后用它来重定向

完整的代码如下所示

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);
}

您甚至可以考虑在使用代码之前验证构建的URL。如果(URI,IsWellFormedUriString(URL,UriKind。绝对))<代码> < /P>因为您手动生成URL,则需要使用<代码> URL。编码(地址+区域+城市+代码)<代码>您需要使用<代码>响应。重定向(URL)在你输入地址后,当客户端已经拥有发出js重定向所需的所有信息时,为什么要进行服务器调用?因为你是手动生成url,所以需要使用

url.Encode(地址+区域+城市+zipCode)
你需要使用
Response.redirect(url)
在您找到地址后,为什么在客户端已经拥有发出js重定向所需的所有信息的情况下进行服务器调用?您能解释一下为什么前面的代码没有像我尝试Url.Ecode as var encode=HttpUtility.UrlEncode(地址+区域+城市+zipCode)那样工作吗并以string.format.As的形式传递,结果是“…”,这个url也被添加到了我的localhost中。至于你的答案是“…”,没有被添加到localhost url中,它按预期打开。你能解释一下为什么前面的代码没有像我尝试url.Ecode As var encode=HttpUtility.UrlEncode那样工作吗(address+Area+city+zipCode)并以string.format.As的形式传递它,结果是“…”,这个url也被添加到了我的localhost中。至于您的答案是“…”,并且没有被添加到localhost url中,该url按预期打开