我想将用户重定向到此链接并能够传递变量
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" })
我已将当前方法添加到localhost链接的Url链接。它给出了错误 - “从客户端(:)检测到一个潜在危险的Request.Path值。” 删除添加的localhost链接
后,url链接(谷歌)可以正常工作答案 0 :(得分:0)
以下是错误的解决方案,"从客户端检测到潜在危险的Request.Path值(:)"
在webconfig文件中尝试以下设置:
<system.web>
<httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0" />
<pages validateRequest="false" />
</system.web>
控制器代码:
如果你想传递变量,请在下面的问号后面给出值
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));
}
答案 1 :(得分:0)
正如评论中已经提到的,网址需要正确构建。
首先构造并编码插入的段。
var segment = string.Join(" ",address, Area, city, zipCode);
var escapedSegment = Uri.EscapeDataString(segment);
然后使用基本格式和转义段
构建完整的URLvar 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与if (Uri.IsWellFormedUriString(url, UriKind.Absolute))