Sunday, April 10, 2016

Asp.net MVC 2 Url.Action example

Asp.Net MVC offers 2 ways of adding Hyperlinks in a page.

1. Using the Html.ActionLink method
2. Using the Url.Action method

In this post we shall see on how to add Hyperliks to a View page using Url.Action. The Url.Action just returns the URL for the specified controller and action, hence while using the Url.Action method we need to explicitly form the tags and text around the Hyperlink as follows.
<a href="<%= Url.Action("Index","User")%>">Register</a>

The Url.Action method can be used in the controller page when you need to get the full URL for a specific Controller and Action. The Url.Action() method can also be used in Ajax requests where we just need the URL to be passed for Ajax requests. For adding a link to a View page Html.ActionLink is prefered over Url.Action

Some examples of using Url.Action are as follows

Simple Hyperlink

<
a href="<%= Url.Action("Index","User")%>">Register</a>

Using as a Button Action Link
<input type="button" value=" Register" onclick="location.href='@Url.Action("
Register", "Index")'" />

Using as a Link to an Image
<a href="@Url.Action("
Register", " Index")"> <img src="Images/Register.gif" /></a>

Search Flipkart Products:
Flipkart.com

1 comment:

Unknown said...

thank you for this tutorialAsp .NET