asp.net mvc 发布到IIS测试,路径的引用问题

之前做的一个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根目录下,这样就可以访问了。

技术有限,没有总结好的请大家指教。

posted @ 2010-01-25 19:34  longgel  阅读(2748)  评论(0编辑  收藏  举报