使用MVC框架中要注意的问题(三):如何为链接指定路径
来源:互联网 发布:手机精美时钟软件 编辑:程序博客网 时间:2024/06/06 07:41
在视图中,我们会用到很多的a和img标签,它们只能接受相对路径。但因为在MVC框架中,视图都是放在不同的文件夹中,那么如何更好滴指定它们的路径呢?
一般的做法是通过.. 这样的方式来向上一级目录回退。这个在一般的视图都能解决。但如果在Index这个Action的时候就会有些麻烦,因为index默认是可以省略的。大家想想看,我们是要写一个.. 来回退一级,还是写两个.. 来回退两级呢?这是一个很麻烦的事情。
我的做法是,用一个变量保存当前网站的虚拟路径,这样就不会有问题了。下面是一个范例
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }>" %> <% var root = Request.Url.GetLeftPart(UriPartial.Authority);%><% foreach (var item in Model) {%><a href='<%= root+"/Photo/Large/"+ Html.Encode(item.Path) %>' title="<%= Html.Encode(item.Title) %>"> <img src='<%= root+"/images/default.png" %>' path='<%= root+"/Photo/Normal/"+ Html.Encode(item.Path) %>' alt="" />a><% } %>
- 使用MVC框架中要注意的问题(三):如何为链接指定路径
- 使用MVC框架中要注意的问题(三):如何为链接指定路径
- 使用MVC框架中要注意的问题(七):HtmlAttributes
- 使用MVC框架中要注意的问题(八):HandleError
- 使用MVC框架中要注意的问题(七):HtmlAttributes
- 使用MVC框架中要注意的问题(八):HandleError
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 使用MVC框架中要注意的问题:如何在页面和用户控件之间传递数据
- 使用MVC框架中要注意的问题(六):何时使用PartialView方法
- 使用MVC框架中要注意的问题(六):何时使用PartialView方法
- 使用MVC框架中要注意的问题:何时使用PartialView(部分类)方法
- 使用MVC框架中要注意的问题(四):ActionLink只是执行Get的操作
- 使用MVC框架中要注意的问题(一):修改首页以支持主题
- 使用MVC框架中要注意的问题(二):将Model和Controller单独用一个项目设计
- MVC中使用aspx当作视图需要注意的问题
- 在tomcat使用中需要注意的路径问题。
- (Linux)链接动态库需要注意的路径问题
- C++中的explicit关键字的作用
- 使用MVC框架中要注意的问题(一):修改首页以支持主题
- 使用MVC框架中要注意的问题(二):将Model和Controller单独用一个项目设计
- Cookie的使用
- 15 Amazing jQuery Image Gallery/Slideshow Plugins and Tutorials
- 使用MVC框架中要注意的问题(三):如何为链接指定路径
- 使用MVC框架中要注意的问题(四):ActionLink只是执行Get的操作
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 实现SQLServer的Session
- 使用MVC框架中要注意的问题(六):何时使用PartialView方法
- 使用MVC框架中要注意的问题(七):HtmlAttributes
- 使用MVC框架中要注意的问题(八):HandleError
- Uninstalling ASP.NET MVC 1.1 after installing Visual Studio 2010 beta 2
- Enabling .NET 3.5 in SharePoint 2007 Sites, the Lazy Way{转载}