CodingQuestions
CodingQuestions

Reputation: 11

Pass in url as parameter in MVC url

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?

Upvotes: 0

Views: 484

Answers (1)

Hita Barasm
Hita Barasm

Reputation: 49

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

Upvotes: 1

Related Questions