views:

55

answers:

2

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.

+1  A: 

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.

Robert Koritnik
Kinda frustrating, that just outputs text... no link
baron
What *exactly* is the output? Do a view-source in your browser and paste it here
Michael Haren
just whatever string I put in the Url.Content("i.e. here") comes out as plaintext, no tags around it. Kind of useless. You would think it would be easy to write a link.
baron
@baron: I thought I was clear about it, but I see I wasn't. Check out my edited answer.
Robert Koritnik
@baron: Asp.net MVC is about total control over your HTML. Url.Content is not used to create links, but translate application web folders to actual request URLs that can be consumed by the client browser. Creating a link **really is easy**. Don't get so frustrated at the very beginning. In time you'll learn that MVC is so much better than WebForms.
Robert Koritnik
thank you. After I studied the routing (and started changing it) this all makes a lot more sense!
baron
+1  A: 

You cannot make a static link to a view page by calling directly the aspx page of the view like

<a href="Views/Home/About.aspx">go</a>

All files inside the Views folder is inaccessible by link directly to a View page. If you want to have a Static link to a View Page, you have to create an Action to a page. But if your page is not inside the Views folder, you can directly call it via link.

Here's a Hierarchy example

MVCProject
    Content
    Controllers
    Models
    StaticPages         <-- all pages in here can be accessible through a direct link <a href="/StaticPages/WebForm.aspx">go</a>
        WebForm.aspx
        |_WebForm.aspx.cs
    Views               <-- all pages in here cannot be accessed directly by calling the Relative path from a link <a href="/Views/Home/Index.aspx">go</a>
        Home
            Index.aspx
            About.aspx

Therefore, if your link is directly calling a page inside the Views it always return 404. Unless you create an ActionResult of that page and Call it by its /Controller/Action url.

rob waminal