Search code examples
htmlasp.net-mvcasp.net-mvc-4razor

Linking text to another page in ASP.NET MVC


I want to redirect to another view in my application when user clicks to the hyperlinked text. I wrote the code like this:

<p class="mb-0">
    <a href="~/Views/Account/ApprovalValidation.cshtml">I want to check approval</a>
</p>

But it's not opening the page and returns an error saying that view cannot found that location.

Can anyone show me what I did wrong?

Here is a picture of the code:

enter image description here


Solution

  • The Url.Action() method is the most convenient way of generating outgoing links:

    <p class="mb-0">
        <a href="@Url.Action("ApprovalValidation", "Account")">I want to check approval</a>
    </p>
    

    Also you can use href="/Account/ApprovalValidation" or href="~/Account/ApprovalValidation". Starting from MVC 4, Razor automatically detects attributes that begin with ~/ and automatically inserts the @Href or @Url call for you.

    See the following article for additional information: Tilde notation maps to the original URLs by using IIS URL rewrite in ASP.NET Web Pages Razor V3