0

Please i am trying to generate my Url hyperlink dynamically in my MVC Application.

<a href='http://facebook.com/myPage'>Facebook</a>

The above link takes me to my facebook home page. I want to be able to change the url later on as the page name is not decided. So i tried

<a href='@Url.Action("GetFaceBookLink", "Home")'>Facebook</a>

And in my controller

 public ActionResult GetFaceBookLink()
        {
            string facebook = repository.SystemSettings.FirstOrDefault().FacebookLink;

            return this.Content(facebook);
        }

My string facebook = "http://facebook.com/mypage" . I want to be redicted to my facebook home page as usal. However, when i clicked, it returns the string url in a black pagewithout redirecting . Please how do i redirect ? how do i achieve this ? Any help would be appreciated.

CBroe
  • 91,630
  • 14
  • 92
  • 150
Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116

2 Answers2

2

You can redirect to another site from controller by

public ActionResult GetFaceBookLink() {
return Redirect("http://facebook.com/mypage");
}
M.Azad
  • 3,673
  • 8
  • 47
  • 77
  • But i coudn't use `Redirect("http://facebook.com/mypage")` for my case though. I got the error Child actions are not allowed to perform redirect actions," . I am doing this in my Layoutpage . A page body has been rendered already and now a controller returning a redirect , i guess thats why the error. Nonetheless , it's a great solution. Thank you :). – Nuru Salihu Apr 11 '15 at 10:59
  • 1
    @NuruSalihu che these url for more info http://stackoverflow.com/questions/2056421/why-are-redirect-results-not-allowed-in-child-actions-in-asp-net-mvc-2 http://stackoverflow.com/questions/25015833/child-actions-are-not-allowed-to-perform-redirect-actions-after-setting-the-sit – M.Azad Apr 11 '15 at 11:15
2

You need Html.Action helper here :

<a href='@Html.Action("GetFaceBookLink", "Home")'>Facebook</a>

Url.Action do not calls action, it generates url using Controller and Action name

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160