更改WebResource.axd的请求URL

Jam*_*Law 8 c# asp.net visual-studio-2008

我的Web应用程序(http://www.something.com/social/competition/)当前正在请求WebResource.axd文件,如下所示:

<script src="/WebResource.axd?d=xxx" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

当我们在Netscaler中使用urlrewiting将"/ social"文件夹的所有请求转发到包含此应用程序的单独服务器场时,"/"根路径将无法正确解析,因为它将从某些内容请求资源.com app.

因此,我需要更改所请求脚本的URL以显式请求它:

<script src="/social/WebResource.axd?d=xxx" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

或使用相对路径请求它:

<script src="WebResource.axd?d=xxx" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经看过覆盖渲染方法,使用控制适配器和其他各种东西,但还没有到目前为止.请帮忙.

Jam*_*Law 10

是的,经过广泛的研究后发现,实际上不可能覆盖这些文件的渲染过程.所以唯一的另一个选择是一个肮脏,肮脏,可怕的黑客!

protected void Page_Load(object sender, EventArgs e)
    {
        //Initialises my dirty hack to remove the leading slash from all web reference files.
        Response.Filter = new WebResourceResponseFilter(Response.Filter);
    }

public class WebResourceResponseFilter : Stream
{
    private Stream baseStream;

    public WebResourceResponseFilter(Stream responseStream)
    {
        if (responseStream == null)
            throw new ArgumentNullException("ResponseStream");
        baseStream = responseStream;
    }

    public override bool CanRead
    {
        get { return baseStream.CanRead; }
    }

    public override bool CanSeek
    {
        get { return baseStream.CanSeek; }
    }

    public override bool CanWrite
    {
        get { return baseStream.CanWrite; }
    }

    public override void Flush()
    {
        baseStream.Flush();
    }

    public override long Length
    {
        get { return baseStream.Length; }
    }

    public override long Position
    {
        get
        {
            return baseStream.Position;
        }
        set
        {
            baseStream.Position = value;
        }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return baseStream.Read(buffer, offset, count);
    }

    public override long Seek(long offset, System.IO.SeekOrigin origin)
    {
        return baseStream.Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        baseStream.SetLength(value);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        //Get text from response stream.
        string originalText = System.Text.Encoding.UTF8.GetString(buffer, offset, count);

        //Alter the text.
        originalText = originalText.Replace("/WebResource.axd", "WebResource.axd");
        //Write the altered text to the response stream.
        buffer = System.Text.Encoding.UTF8.GetBytes(originalText);
        this.baseStream.Write(buffer, 0, buffer.Length);

    }
Run Code Online (Sandbox Code Playgroud)

这将拦截流到页面并将所有出现的"/WebResource.axd"替换为"WebResource.axd".因为它是一条相对的道路,它可以很好地解决!

另一个解决方案是我需要将Web应用程序安装到模拟"/ social"关键字重定向的虚拟目录.这将导致asp.net更新HttpRuntime.AppDomainAppVirtualPath以在页面上的引用中包含"/ social",因此将正确解析.

呐喊呐喊!

  • originalText = originalText.Replace(HttpContext.Current.Request.ApplicationPath +"/",VirtualPathUtility.MakeRelative(HttpContext.Current.Request.Url.AbsolutePath,"〜/")); 这一行将使所有绝对链接相对链接,获得正确的../元素. (2认同)
  • @Punit您需要在IIS中创建一个虚拟应用程序并在那里托管您的代码.例如,在我的情况下,我会在我的根网站上创建虚拟应用程序"social",指向应用程序,ergo将http:// localhost/social/behave作为应用程序根目录. (2认同)