0

I want to pass in a url to an MVC url like so:

https://localhost:port/info/[url]

I am passing in the [url] as a url encoded string and I still get a 404 error.

So the resulting url is:

https://localhost:42901/info/http%3A%2F%2Fwww.google.com

And I want the info Action to be able to access http%3A%2F%2Fwww.google.com and ultimately forward to the passed in url.

Is this possible with MVC 5? If so, is it an IIS setting?

2
  • What is the resulting URL that you are requesting? Did you mean a "URL encoded string"? Please provide specific details about the problem. Commented Aug 30, 2021 at 19:23
  • 2
    Well, you don't want a URL decoded string -- you want it encoded, since it's going in a URL. Please edit your question to include a minimal reproducible example and the rendered code. Commented Aug 30, 2021 at 19:25

1 Answer 1

1

Consider you have a controller named HomeController and it contains various actions. One of these actions might be Info as you called it in your URL.

A sample code of this controller will be like:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Info(string url)
    {
        return RedirectPermanent(url);
    }
}

As you see, redirecting is very simple! Just you have to make a little corrections in calling controllers and actions.

So, you need to remember two things! First, your URL must contain controller's name (Home) and second, it is better to pass parameters by question mark and equal sign (?parameter=). So your url would be like this:

https://localhost:42901/Home/Info?url=http%3A%2F%2Fwww.google.com

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

3 Comments

Thanks for the great explanation and solution. Is there a particular reason I can't use localhost:42901/Home/Info/http%3A%2F%2Fwww.google.com? Why is it better to pass parameters by querystring instead of parameter?
@CodingQuestions That's because your second url (google url) gets parsed before redirecting by IIS url interpreter, so this url you have entered will be something like: localhost:44355/Home/Info/http:/www.google.com. Then you see after http it contains an slash (/) character. This confuses IIS to find out which part of url should be given to action parameter... I hope that my explanation helps you. :)
Also, see this tutorial: c-sharpcorner.com/UploadFile/1e050f/… . String parameters cannot be pass in the form that you are trying. You must do it in the way how I explained in the answer.

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.