5

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/Subscription?userId=@Model.User.Id">

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

Colin
  • 1,987
  • 3
  • 17
  • 21

4 Answers4

11

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

rBalzer
  • 347
  • 1
  • 7
  • 1
    You don't have to inject IHostingEnvironment into the controller if you just want to use it in a view. InjectingIHostingEnvironment into _ViewImports makes sense, if you want it to be available in any View. If you just need it in a single view, you can inject it directly into that view. – René Mar 12 '19 at 10:57
  • 2
    use `IWebHostEnvironment` in place of `IHostingEnvironment` for asp.net core 3.1 and later versions. – yogihosting Jun 10 '21 at 10:31
2

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.

Colin
  • 1,987
  • 3
  • 17
  • 21
2

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)
René
  • 3,413
  • 2
  • 20
  • 34
0

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