ASP.NET Hyperlinks

Hello,

I’ve just started playing around with ASP.NET.

I’m using the MVC model of ASP.NET. I’m working on creating the main master right now.

I created most of my stuff with good old HTML, except for the ContentPlaceHolders.

I created the hyperlinks with regular anchor tags. I created my various controllers and views for each of my main pages.

In the anchor tags, I placed the URLs that would be used for in HTML. For example, I have a page called “Register”, so I have a controller “RegisterController”, then it’s Index View.

My question is: The code editor tells me it can’t find the file in the href attribute. Is there a better way to do hyperlinks in ASP.NET, or do I just need to deal with those warnings?

Thanks.

Look at the Html.ActionLink method. It’s an extension method of the HtmlHelper class for views. For example, to link back to the homecontroller.index action…

<%: Html.ActionLink(“Back to Index”, “Index”, “Home”) %>

There are several overrides with all the needed parameters.

<html>

<script language=“C#” runat=server>

void Page_Load(Object sender, EventArgs e) {
// Set hyperlink to “~”, which indicates application root.
HyperLink1.NavigateUrl = “~”;
}

</script>

<body>

&lt;h3&gt;&lt;font face="Verdana"&gt;Simple asp:hyperlink Sample&lt;/font&gt;&lt;/h3&gt;

&lt;form runat=server&gt;

    &lt;p&gt;

    &lt;asp:hyperlink id=HyperLink1 runat="server"&gt;
        Go To QuickStart
    &lt;/asp:hyperlink&gt;

&lt;/form&gt;

</body>

</html>

This code May help you.

No, that code doesn’t help when you are in MVC. Really doesn’t help when you are not in MVC too, but that’s a different story.

Best bet for anything MVC is to use Html.ActionLink or it’s cousin Url.Action.

I’m learning ASP.net 4 as well, not the MVC part yet.

If you click on the C:\ part in the solution explorer and hit F4 it will bring up the properties window. Change the virtual path to a /, and make sure you point to the file path like this:

url=“~/Default.aspx”