开发者

Site Navigation in ASP.NET MVC2

I'm new to MVC and developing a basic site using the pattern. I'm trying to implement left navigation that will be consistent throughout the site... it is basically a few ul's styled with CSS and using jquery for some visual effects. I have a few questions regarding best design practices:

  1. Is it best to have the html for the navigation included directly in the master page I am using? Or is开发者_运维技巧 there a better way to do it to fit into the MVC pattern?

  2. If it is best to include the html directly in the master page, what is the best way to achieve absolute URLs? For example, to go to the about page, it will always be mysite.com/about/, but MVC doesn't support the ~ that I'm used to in ASP.NET forms. I don't want to explicitly type the domain because that will mess up my dev environment (where my domain will be different than in production).

Thanks!


If the links on the side are static then yes putting then directly in the master page is fine. If they differ based on which page you're looking at you should consider making a Model to represent the left hand navigation, probably with a collection of links, which is populated by the appropriate controller action and passed to the view.

In terms of absolute URLs, you should use the Url.Action method which will translate controller actions into Urls for you based on the RouteTable. If you update your routing your URLs won't break:

<a href="<%= Url.Action("Index", "About")">Home</a>

Will make a link to /About (assuming you're running in the root of the website and you're using the default routes.

<a href="<%= Url.Action("Post", "Blog", new { Id = 5 } )">Read more</a>

Will take you to /Blog/Post/5.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜