2009-03-06 39 views
1

我已將應用程序部署到IIS6服務器。目前,我正在使用通配符映射。我的應用程序在我的開發機器上工作得很好,但是當我嘗試在服務器上訪問它時,有些頁面可以工作,有些則不能。爲什麼我的圖片/腳本不會顯示在asp.net mvc部署的站點上?

這是腳本&圖像,給我最大的問題。

我有一個網址http://localhost/sdev/home/index和頁面出現良好,除非圖像和腳本不加載。當我查看源代碼,並期待在網址我看到:

../../Content/Images/logo.png 

如果我嘗試導航到該網址,它試圖去

http://localhost/content/images/logo.png 

,而不是

http://localhost/sdev/content/images/logo.png 

的奇怪的是,有些頁面正常工作,如:

http://localhost/sdev/ServiceCall/DivisionStep/ALB?type=fsr 

關於我能做些什麼來解決這個問題的想法?是的,我已閱讀菲爾的指示,並認爲我正確地跟着他們,但也許我錯過了一些東西。

+0

是否運行了MVC的版本? RC1? RC1刷新? RC2? – 2009-03-06 22:01:39

+0

我正在運行RC2。 – 2009-03-06 22:04:17

回答

1

使用

<%= Url.Content("~/Content/Images/logo.png") %> 

生成的URL,你應該沒問題。

0

不是這樣:

../../Content/Images/logo.png 

這樣做:

/sdev/Content/Images/logo.png 

更妙的是,生成URL的第一部分(/ SDEV)在後面的代碼,因爲它聽起來也許這部分會改變(我猜這裏「sdev」是某種開發版本的網站,並且對於製作網站前面不會有「sdev」?)

它之所以不適合我在第一個例子中,rk是瀏覽器看到它,就好像您正在查看「sdev/home」目錄中名爲「index」的文件。因此,向上兩個目錄會將您帶到根級別。

對於「/ sdev/ServiceCall/DivisionStep/ALB」,它工作正常因爲您現在正在查看「/ sdev/ServiceCall/DivisionStep」目錄中的「ALB」,並且向上兩級會將您帶到「/SDEV」

1

我只是寫了一些助手的圖像,你可以使用。 (1)只需創建一個名爲AppHelper的公共靜態類,然後將其添加到名爲'Helpers'的MVC項目中的文件夾中。然後將其添加到using System.Web.Mvc;中。

(2)複製在這些方法中:

public static string Image(this HtmlHelper helper, 
     string classText, string sourcePath, string altText, string width, string height) 
    { 
     return Image(helper, classText, sourcePath, altText, width, height, null); 
    } 
public static string Image(this HtmlHelper helper, 
     string classText, string sourcePath, string altText, string width, string height, object htmlAttributes) 
    { 
     StringBuilder sb = new StringBuilder(); 
     if (htmlAttributes != null) 
      foreach (PropertyInfo p in htmlAttributes.GetType().GetProperties()) 
       sb.AppendFormat(@" {0}=""{1}""", p.Name, p.GetValue(htmlAttributes, null).ToString()); 

     if (htmlAttributes == null) 
      return String.Format(@"<img{0} src=""{1}"" alt=""{2}"" width=""{3}"" height=""{4}"" />", 
       String.IsNullOrEmpty(classText) ? String.Empty : String.Format(@" class=""{0}""", classText), 
       (new UrlHelper(helper.ViewContext.RequestContext)).Content(sourcePath), 
       altText, width, height); 
     else 
      return String.Format(@"<img{0} src=""{1}"" alt=""{2}"" width=""{3}"" height=""{4}""{5} />", 
       String.IsNullOrEmpty(classText) ? String.Empty : String.Format(@" class=""{0}""", classText), 
       (new UrlHelper(helper.ViewContext.RequestContext)).Content(sourcePath), 
       altText, width, height, sb.ToString()); 
    } 

(3)..並使用像這樣: <% =Html.Image("small_pic_border","~/Content/Images/Home/office2_137x139.jpg","principal headshot","137","139") %>

此方法使用liammclennan提到的Url.Content方法。還應該強迫你一些很好的習慣所:喜歡使用替代文本等

對於腳本中使用: <script type="text/javascript" src="<% =Url.Content("~/Scripts/mootools.js") %>"></script>

相關問題