Search code examples
asp.net-coreasp.net-core-mvcvirtual-directory

How to get virtual directory in mvc


I am working on a .net core application where I need the application path to the virtual directory. I do not want the physical path, I want it in form of url like:

http://www.mywebsite.com/mydirectory

I have tried with Request.Host but it only gives me http://www.mywebsite.com without the virtual directory path.

Any help on this appreciated!


Solution

  • You can use the IUrlHelper to resolve the base-path of the application. If the application is not hosted within a virtual directory the returned path is just empty...

    IUrlHelper url = ...
    string virtualDirectory = url.Content("~");