2012-07-20 78 views
0

相对位置我有一个文件结构如下所示:更新在ASP.NET MVC

/root 
    /images 
    image1.png 
    image2.png 
    button1.png 
    button2.png 
    /css 
    app.css 
    core.css 
    /scripts 
    jquery-1.6.4.min.js 
    jquery.mobile-1.1.0.min.js 
    /path1 
    /pathA 
     index.cshtml 
     page1.cshtml 
    index.cshtml 
    /path2 
    /pathA 
     index.cshtml 
     page1.cshtml 
    /pathB 
     index.cshtml 
     page1.cshtml 
    index.cshtml 
    index.cshtml 
    _layout.cshtml 

所有.cshtml文件依赖_layout.cshtml的共享内容。我的_layout.cshtml文件如下所示:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <title></title> 

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"> 
    <meta name="apple-mobile-web-app-capable" content="yes"> 

    <link rel="stylesheet" href="/css/core.css" />  
    <link rel="stylesheet" href="/css/app.css" /> 

    <script src="/scripts/jquery-1.6.4.min.js" type="text/javascript"></script> 
    <script src="/scripts/jquery.mobile-1.1.0.min.js" type="text/javascript"></script> 

    <style type="text/css"> 
     #button1 .ui-icon { background: url(/images/button1.png) } 
     #button2 .ui-icon { background: url(/images/button2.png) }  
    </style> 

</head> 
<body> 
    @RenderBody() 
</body> 
</html> 

此文件以“/”开头引用所有URL。不幸的是,当我将应用程序移动到PhoneGap时,这不起作用。我希望用相对路径替换所有开始的“/”。举例来说,如果我访问/path1/pathA/index.cshtml我想这两个按钮CSS定义的样子:

#button1 .ui-icon { background: url(../../images/button1.png) } 
    #button2 .ui-icon { background: url(../../images/button2.png) }  

有没有一种方式,使他们相对的,我可以自动改变我的路径一样如上所示?我想要什么?

回答

3

我建议你使用内置的HTML辅助,例如:

<script src="@Url.Content("~/Scripts/jquery-1.6.4.min.js")" type="text/javascript"></script> 

...

#button1 .ui-icon { background: url(@Url.Content("~/Content/images/button1.png")) } 
+0

我如何引用Url.Content?当我尝试时,我得到一个运行时错误,说:“CS0103:名称'Url'在当前上下文中不存在”。我看到这个:http://stackoverflow.com/questions/3912508/using-url-content-in-asp-net-mvc-2-0但我不知道如何继承_Layout.cshtml中的东西。啊。 – user70192 2012-07-20 17:17:53

+0

您使用的是什么版本的mvc?我不认为你需要在剃刀视图中继承某些东西,那是用于带有webform视图的mvc2 – 2012-07-20 18:01:28

+0

我正在使用ASP.NET MVC 3 – user70192 2012-07-20 18:09:03