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 directl开发者_StackOverflow中文版y 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.
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.
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.
精彩评论