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

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

问题描述

所以,ASP.net有一个应用程序根的概念。它是对应于被设置为IIS中的应用程序的根目录的URL的路径部分。波浪号字符(〜)映射到ASP.net的URL这条道路,所以如果ASP.net认为我的应用程序是在/ MyApp的,东西服务器控件的URL,我给为〜/脚本/的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重写,使其可用而不prefixing目录名,但ASP.net始终是$ P $反正pfixing的目录名的任何地方我用的〜。我真想让〜决心为空字符串。人能做到呢?

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天全站免登陆