在视图中,我们会用到很多的a和img标签,它们只能接受相对路径。但因为在MVC框架中,视图都是放在不同的文件夹中,那么如何更好滴指定它们的路径呢?

一般的做法是通过.. 这样的方式来向上一级目录回退。这个在一般的视图都能解决。但如果在Index这个Action的时候就会有些麻烦,因为index默认是可以省略的。大家想想看,我们是要写一个.. 来回退一级,还是写两个.. 来回退两级呢?这是一个很麻烦的事情。

我的做法是,用一个变量保存当前网站的虚拟路径,这样就不会有问题了。下面是一个范例

 

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Web.Models.PhotoListItem>>" %>
<% 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>
<% } %>

相关文章:

  • 2021-09-03
  • 2021-06-23
  • 2021-09-15
  • 2022-12-23
  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-03-10
  • 2021-12-08
  • 2021-09-07
  • 2022-12-23
  • 2021-06-26
  • 2021-05-30
  • 2022-12-23
相关资源
相似解决方案