Search code examples
asp.netasp.net-mvcasp.net-mvc-2actionlink

Working with links asp.net mvc2


How do I make a static link in a view? (using ASP.NET MVC2)

I am working on the site navigation, and have basically 4 main areas. These are split into sub areas, and one controller looks after the entire "main area".

So basically after clicking on a main area, i'd like to display a list of links to the different sub areas. I created the pages just by right clicking and adding views then putting a list of links on them. But how do I link to this with the <%: Html.ActionLink %> ... it seems I can't link directly to .aspx's

edit:

I have tried <%: Url.Content("~/Path/to.aspx") %> and that is just outputting text...

edit: when I link directly to it just using <a href="......aspx">Go</a> and click on it, I get

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

But problem is I know it is definetly there.


Solution

  • Static content links

    If you're linking directly to actual files (not MVC views) than you can just statically write them down or use <%= Url.Content() %> to make them more application root folder aware.

    So a certain static link with application root folder awareness would look like this:

    <a href="<%= Url.Content("~/some/folder/file.aspx") %>">Go there</a>
    

    MVC views

    But if you're referring to views, then you should simply provide controller and action names to your Html.ActionLink() call. It should translate to your routing settings and to the right controller action which decides which view (or in other words which ASPX) to show.

    Just to make it clear: You don't link to views in MVC because they are forbidden from direct access (check web.config file in the /Views folder). Instead you always link to controller actions that serve view content as their action result.