开发者

Changing ASP.net application root?

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".

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 p开发者_Python百科refixing 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?


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; } }
}


So you have a Site setup in IIS, and then you've created a "Application" inside the site. You want the "Application" to resolve your CSS/JavaScript/Images to the root directory of the site, not to the sub Application?

Why do you need to put the tilde in to begin with?

Why not just do

"/scripts/script.js" "/css/main.css"

So it always comes from the root directory.


You won't be able to do what you want with the way you have your site/application set up. The application root is just that, the path to the root of your application. If you want your "website" and your "app" to both have "/" as the application root, you'll have to create two websites and make them listen to different host headers (ex: mysite.com and myapp.mysite.com). This way the application root is the root of the website, "/", and not the root of the application folder.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜