Cemre Mengü
Cemre Mengü

Reputation: 18754

Using href tag in ASP.NET

I am new to ASP.NET. In my content file I have the line:

<a href="products/myproduct">

I have a view file named Myproduct.aspx and inside the ProductsController there is the method:

   public ActionResult Myproduct()
    {
        return View();
    }

However for the line <a href="products/myproduct"> Everything works fine but I get a warning saying the path products/myproduct does not exist. Am I doing something wrong ? Is this the right way to achieve this ?

Upvotes: 0

Views: 474

Answers (2)

Kevin Brechb&#252;hl
Kevin Brechb&#252;hl

Reputation: 4727

You should create your link like this:

<%= Html.ActionLink("Go to product", "Myproduct", "Products") %>

According to the MSDN documentation the first parameter is the linkText, second is the actionName and third is the controllerName.

This way you don't need to write the <a href="">on your own. If you want to write this on your own (but it's not recommended), you need to use the Url.Action() method:

<a href="<%= Url.Action("Myproduct","Products") %>">Go to product</a>

Upvotes: 1

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

You might want to use Url helpers instead:

<a href="@Url.Action("MyProduct","Products")">

Upvotes: 1

Related Questions