我在不同的JS对象创建的处理程序,如这输出一些环境参数进行再利用:
/Environment.ashx/.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication2
{
///
/// Summary description for $codebehindclassname$
///
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;
}
}
}
}
启用您的网页的处理程序。配置:
设置你的主人或内容ASPX页面(如下假设ASP.NET MVC):
现在,对于承担Environment.ashx所有的JS对象和脚本已经载入,你可以参考Environment.RootPath
var imagePath = Environment.RootPath + "images/image.jpg";