Colin
Colin

Reputation: 2011

How do I get the Web Root Path and the Content Root Path in ASP.NET Core?

I am trying to add a root path as a parameter in a View, so I can pass it as a parameter to a PayPal button.

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
    ... snip ...

    <input type="hidden" name="return" value="@Model.UrlRoot/Manage/[email protected]">

    ... snip ...
</form>

I was sifting through the answers at How can I get my webapp's base URL in ASP.NET MVC? (20 answers) and ASP.NET MVC 6 application's virtual application root path

Since ASP.NET Core is quite different, the Request class no longer contains a .Url property, so most of those answers don't work.

Upvotes: 5

Views: 20743

Answers (5)

raddevus
raddevus

Reputation: 9087

I'm here in 2024 running .NET Core 8.x and now the following is possible and far easier.

In your Controller add the following parameter (IWebHostEnvironment webHostEnvironment) to your Controller's constructor:

private string webRootPath;  // member var
private string contentRootPath; // member var

public UserController(ILogger<HomeController> logger, IWebHostEnvironment webHostEnvironment)
{
     _logger = logger;
     Console.WriteLine($"content rootPath: {webHostEnvironment.WebRootPath}");
     webRootPath = webHostEnvironment.WebRootPath;  // wwwroot directory
     contentRootPath = webHostEnvironment.ContentRootPath; // csproj directory (one up from wwwroot)
    }

Now, you'll be able to use the webRootPath and contentRootPath vars to pass the values into your View from any method in your Controller.

Upvotes: 1

Gary Jorgenson
Gary Jorgenson

Reputation: 61

In a net core 5 razor page, I added the following to the top of the page:

@inject IHostEnvironment hostEnvironment

then further down in my code, I used the following code with success:

string filePath = $@"{ hostEnvironment.ContentRootPath }\wwwroot\assets\img\users\{ user.ID }.jpg";

    if (System.IO.File.Exists(filePath))
    {
        imageURL = $"{ user.ID }.jpg";
    }

Upvotes: 0

Ren&#233;
Ren&#233;

Reputation: 3711

With .NET core 2.1 the context is automatically injected into views. The base path of the request which displayed the view can be accessed like this:

@(Context.Request.PathBase)

Upvotes: 3

rBalzer
rBalzer

Reputation: 367

You can inject the IHostingEnvironment into the Controller like this:

   public class HomeController : Controller
    {
        protected readonly IHostingEnvironment _hostingEnvironment;

        public HomeController(IHostingEnvironment hostingEnvironment)
        {

        }
    }

In your _ViewImports.cshtml add:

@using Microsoft.AspNetCore.Hosting
@inject IHostingEnvironment HostingEnvironment

Now you can use can use HostingEnvironment and all its properties in your form. For example HostingEnvironment.WebRootPath or HostingEnvironment.ContentRootPath

Upvotes: 11

Colin
Colin

Reputation: 2011

I came across Marius Schulz's post. (If you are Marius, please add your answer, contact me and I'll remove mine.)

https://blog.mariusschulz.com/2016/05/22/getting-the-web-root-path-and-the-content-root-path-in-asp-net-core

For some reason my Controllers don't have the IHostingEnvironment injected in the constructor, but they do have the Request object.

In my Controller, I've declared

var urlRoot = $"{Request.Scheme}://{Request.Host}{Url.Content("~")}";

and passed it to MyViewModel

var model = new MyViewModel { UrlRoot = urlRoot };
return View(model);

This takes into account http vs. https, port numbers, and hopefully, the site root if the web site is not rooted at /. Since my site is at / I cannot confirm that Url.Content("~") gets the site root.

Upvotes: 2

Related Questions