C# 虚拟目录的绝对路径引用问题
当存在虚拟目录时,My.js文件中的My image路径引用(src='/images/image.jpg')会失败,因为虚拟目录包含在路径层次结构中 我可以在我的应用程序的其他区域处理这个问题,在这些区域中,我有服务器方法为我提供正确的路径信息C# 虚拟目录的绝对路径引用问题,c#,asp.net,javascript,C#,Asp.net,Javascript,当存在虚拟目录时,My.js文件中的My image路径引用(src='/images/image.jpg')会失败,因为虚拟目录包含在路径层次结构中 我可以在我的应用程序的其他区域处理这个问题,在这些区域中,我有服务器方法为我提供正确的路径信息 什么是处理.js文件的好方法?我创建了一个这样的处理程序来输出一些环境参数,以便在各种js对象中重用: /Environment.ashx/.cs using System; using System.Collections.Generic; usin
什么是处理.js文件的好方法?我创建了一个这样的处理程序来输出一些环境参数,以便在各种js对象中重用: /Environment.ashx/.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication2
{
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
public class Environment : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/javascript";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(600));
context.Response.Write(String.Format("Environment = {{ RootPath:\"{0}\" }};", context.Request.ApplicationPath)); // set any application info you need here
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
我创建了这样一个处理程序来输出一些环境参数,以便在各种JS对象中重用: /Environment.ashx/.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication2
{
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
public class Environment : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/javascript";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(600));
context.Response.Write(String.Format("Environment = {{ RootPath:\"{0}\" }};", context.Request.ApplicationPath)); // set any application info you need here
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
引用将来自js文件的位置;不是从应用程序根目录。假设您的根目录下有一个js目录,请尝试添加。。在图像的路径中(../images/image.jpg) 引用将来自js文件的位置;不是从应用程序根目录。假设您的根目录下有一个js目录,请尝试添加。。在图像的路径中(../images/image.jpg) 我假设您正在运行一种Unix风格。你有两个选择,我肯定会奏效,还有一个我不确定:
我假设您运行的是Unix。你有两个选择,我肯定会奏效,还有一个我不确定:
你能把路径参数传递给你的函数吗
// in the code behind
String imagePath = Page.ResolveClientUrl("~/images/");
...
<%-- in mark up --%>
doSomething('<%= imagePath %>');
...
// in js
function doSomething(path) {
var imageSrc = path + '/image.jpg';
}
//在代码隐藏中
字符串imagePath=Page.ResolveClientUrl(“~/images/”);
...
doSomething(“”);
...
//在js中
函数doSomething(路径){
var-imageSrc=path+'/image.jpg';
}
将有一些方法使其更优雅,但这是一般的想法。您能将路径参数传递给您的函数吗
// in the code behind
String imagePath = Page.ResolveClientUrl("~/images/");
...
<%-- in mark up --%>
doSomething('<%= imagePath %>');
...
// in js
function doSomething(path) {
var imageSrc = path + '/image.jpg';
}
//在代码隐藏中
字符串imagePath=Page.ResolveClientUrl(“~/images/”);
...
doSomething(“”);
...
//在js中
函数doSomething(路径){
var-imageSrc=path+'/image.jpg';
}
有一些方法可以使它更优雅,但这是一般的想法。是的,OP需要使用相关路径是的,OP需要使用相关路径因为他使用的是ASP.Net,我怀疑他没有使用任何风格的Unix。因为他使用的是ASP.Net,我怀疑他没有使用任何风格的Unix。
// in the code behind
String imagePath = Page.ResolveClientUrl("~/images/");
...
<%-- in mark up --%>
doSomething('<%= imagePath %>');
...
// in js
function doSomething(path) {
var imageSrc = path + '/image.jpg';
}