更改 ASP.net 应用程序根目录? [英] Changing ASP.net application root?

查看:36
本文介绍了更改 ASP.net 应用程序根目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,ASP.net 具有应用程序根"的概念.它是 URL 的路径部分,对应于为 IIS 中的应用程序设置的根目录.波浪号字符 (~) 映射到 ASP.net URL 中的那个路径,所以如果 ASP.net 认为我的应用程序在/MyApp,那么服务器控件中的某些东西我给出的 URL 为~/Scripts/script.js"将解析到(并被发送到浏览器)/MyApp/Scr​​ipts/script.js".

So, ASP.net has the concept of an 'application root'. It is the path part of the URL that corresponds to the root directory that is set for an application in IIS. The tilde character (~) maps to that path in ASP.net URLs, so if ASP.net thinks my application is at /MyApp, something in a server control whose URL I give as "~/Scripts/script.js" will resolve to (and be sent to the browser as) "/MyApp/Scripts/script.js".

这是一个远景,但有没有办法可以任意更改此应用程序根目录?我实际上在另一个目录下的目录中有一个应用程序,并且我正在使用 URL 重写使其可用而无需为目录名称添加前缀,但是 ASP.net 总是在我使用 ~ 的任何地方添加目录名称的前缀.我真的想让 ~ 解析为空字符串.可以吗?

This is a long shot, but is there a way I can change this application root arbitrarily? I actually have an app in a directory under another one and I'm using URL rewriting to make it available without prefixing the directory name, but ASP.net is always prefixing the dir name anyway anywhere I use ~. I really want to make ~ resolve to an empty string. Can one do it?

推荐答案

您应该能够通过编写自定义 VirtualPathProvider 来更改 ~ 映射到的语义.这是它可能的样子.我已经在一个简单的案例上对此进行了测试,但它可能需要完善.

You should be able to change the semantic of what ~ maps to by writing a custom VirtualPathProvider. Here is what it might look like. I have tested this on a simple case, but it probably needs polishing.

我建议您先试用一个简单的测试应用程序,然后再将其移动到您的真实场景中.这样可以更轻松地隔离问题并对其进行迭代.

I suggest you play around with it an a simple test app before you move it to your real scenario. That'll make it easier to isolate issues and iterate on it.

using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;

public class TildeModifyingVPP : VirtualPathProvider {

    // Change this to what you want ~ to map to
    private const string PseudoRoot = "~/PseudoAppRoot/";

    public static void AppInitialize() {
        HostingEnvironment.RegisterVirtualPathProvider(new TildeModifyingVPP());
    }

    private string ResolvePath(string virtualPath) {
        // Make it app relative, i.e. ~/...
        virtualPath = VirtualPathUtility.ToAppRelative(virtualPath);

        // Change the ~/ to our pseudo root
        return PseudoRoot + virtualPath.Substring(2);
    }

    public override bool FileExists(string virtualPath) {
        return base.FileExists(ResolvePath(virtualPath));
    }

    public override VirtualFile GetFile(string virtualPath) {
        return new DelegatingVirtualFile(virtualPath, base.GetFile(ResolvePath(virtualPath)));
    }

    public override bool DirectoryExists(string virtualDir) {
        return base.DirectoryExists(ResolvePath(virtualDir));
    }

    public override VirtualDirectory GetDirectory(string virtualDir) {
        return new DelegatingVirtualDirectory(virtualDir, base.GetDirectory(ResolvePath(virtualDir)));
    }

    public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) {
        virtualPathDependencies = virtualPathDependencies.Cast<string>().Select(vpath => ResolvePath(vpath));
        return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
    }
}

class DelegatingVirtualFile : VirtualFile {
    private VirtualFile _underlyingFile;
    public DelegatingVirtualFile(string virtualPath, VirtualFile underlyingFile): base(virtualPath) {
        _underlyingFile = underlyingFile;
    }

    public override Stream Open() {
        return _underlyingFile.Open();
    }
}

class DelegatingVirtualDirectory : VirtualDirectory {
    private VirtualDirectory _underlyingDir;
    public DelegatingVirtualDirectory(string virtualPath, VirtualDirectory underlyingDir)
        : base(virtualPath) {
        _underlyingDir = underlyingDir;
    }

    public override IEnumerable Children { get { return _underlyingDir.Children; } }
    public override IEnumerable Directories { get { return _underlyingDir.Directories; } }
    public override IEnumerable Files { get { return _underlyingDir.Files; } }
}

这篇关于更改 ASP.net 应用程序根目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆