asp.net mvc 發布到IIS測試,路徑的引用問題

來源:互聯網
上載者:User

之前做的一個ASP.NET MVCI應用程式,今天發布到IIS中進行測試,結果發現之前的很多引用的指令檔和CSS檔案都顯示不正常,仔細一看原來是路徑引用的問題。

IIS裡建的虛擬目錄,但我在引用檔案的時候都是使用的是網站根目錄的方式,當時沒有考慮到,我是菜鳥大家別笑,記錄下來,是提醒以後別忘記了,順便提醒一下馬虎的朋友們,^_^。

突然碰到這個問題,之前又沒有太在意。問題總得想辦法解決呀,最主要的就是三個問題。

1.html中引用的一些路徑問題:

我才突然想到asp.net mvc中的一個協助類UrlHelper,就使用了Url.Content()方法,使用了表示asp.net 中的虛擬根目錄符號”~”,這樣才將問題解決了,這個符號在一般的html代碼中是不可以正常使用的,一定要在asp.net 中才可以使用。

2.指令碼中引用路徑的問題

指令碼中也需要使用一些url,我是這樣的,我寫了一個WebHandler讓它返回一個javascript 字串對象,這個對象的字串是指網站根目錄或者虛擬目錄。

WebHandler中的代碼

 

代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace ZencartFAQ.Handler
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetRootPath : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if ("/Handler/GetRootPath.ashx".ToUpper() == context.Request.ServerVariables["SCRIPT_NAME"].ToUpper())
            {
                context.Response.Write("var rootPath='" + "http://" + context.Request.ServerVariables["HTTP_HOST"] + "'");
            }
            else
            {
                string virtualPath = context.Request.ServerVariables["SCRIPT_NAME"];
                int tmpEnd = virtualPath.IndexOf('/', 1);
                context.Response.Write("var rootPath='" + "http://" + context.Request.ServerVariables["HTTP_HOST"] + virtualPath.Substring(0, tmpEnd + 1) + "'");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

另外指令碼的使用就是引用

代碼

    <script src='<%=Url.Content("~/Handler/GetRootPath.ashx")%>' type="text/javascript" language="javascript"></script>
    <script type="text/javascript" language="javascript">
        alert(rootPath);
    </script>

3.樣式表中引用的問題

CSS中一般引用路徑都是一些圖片檔案,我是將所有的圖片檔案放到IIS根目錄下,這樣就可以訪問了。

技術有限,沒有總結好的請大家指教。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.