Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 332
Jez Avatar asked Dec 03 '10 22:12

Jez


People also ask

What is override application root URL?

Override application root URL doesn't change where the Application root is within your application. It changes the URL used to reach the application root. Because IIS does some hostname verification it's used to specify if you want to reach your application by a means other than localhost:[PORT] .

How do I get the root path in .NET core?

The Application Base (Root) path is available in the ContentRootPath property of the interfaces IHostingEnvironment (. Net Core 2.0) and IWebHostEnvironment (. Net Core 3.0) in ASP.Net Core. The IHostingEnvironment is an interface for .


1 Answers

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; } }
}
like image 167
David Ebbo Avatar answered Sep 19 '22 23:09

David Ebbo