0

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 ?

2
  • Does it work if you use a .cshtml file instead if a .aspx one? Commented May 31, 2014 at 21:45
  • What is the warning you get? What are you trying to do? Commented May 31, 2014 at 22:10

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

What is the aspx way to do it ?
@Cemre you need to add <%= and %>. I've updated my answer.
I do <a href="<%= Html.ActionLink("Go to product", "Myproduct", "Products") %>"> but it does not work for some reason. The link output becomes <a%20href=` and I get a potentially dangerous link error. Maybe I am using it wrong ?
@Cemre you don't need to add the <a href="">, this will be automatically rendered. If you wan't to add the a-tag on your own, then you must to to use Url.Action() instead. I've also added this to my answer.
1

You might want to use Url helpers instead:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.