1

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 (:)." and the url link(google) does work once I remove the added localhost link

3
  • 2
    Since you're manually generating the url, you need to use Url.Encode(address + Area + city + zipCode) Commented Jun 29, 2017 at 17:22
  • you need to use Response.Redirect(url) after you concat the address Commented Jun 29, 2017 at 17:27
  • Why are you making a server call when the client side already has all the information it needs to just issue a js redirect? Commented Jun 30, 2017 at 2:06

2 Answers 2

1

As already mentioned in the comments the url needs to be properly constructed.

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the solution for the error,"A potentially dangerous Request.Path value was detected from the client (:)"

Try these settings in webconfig file:

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

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.