C# Question

How to redirect a user to a new website in MVC ? Redirect appends return url to a controller method

I want the user to be redirected to a particular URL. In my case,

www.madhu.co.in
.

My controller action looks like this:

[HttpPost]
public ActionResult Redirec()
{
return Redirect("www.madhu.co.in");
}


I'm calling this via a
httpPost
call.

The caller website is
http://localhost/home
.

After this method is invoked, MVC tries to redirect to
http://localhost/home/www.madhu.co.in
.

It is very weird.

Why does MVC append my external URL to the base url ?

However, when I re-define the method to contain a
http://
, MVC redirects to
http://www.madhu.co.in
.

[HttpPost]
public ActionResult Redirec()
{
return Redirect("http://www.madhu.co.in");
}


Why is this discrepancy ? Should I always prefix a url with
http://
to redirect to a new page ? If not, it will redirect to a
action method
within controller?

Plainly confused. Any insights ?

Answer

Any URL that doesn't have a protocol (http://) or a forward slash (/) is considered a relative URL. It doesn't matter if it starts with www, that's just considered an arbitrary subdomain.

Since it's considered relative, it gets appended to the beginning of the end of your existing URL. So in order to redirect off site, you need a fully qualified absolute URL.