Reputation: 98
I have following code in _Layout.cshtml
@Styles.Render("~/Content/css")
@functions{
public static string GetRootPath()
{
return string.Format("{0}", HttpRuntime.AppDomainAppVirtualPath == "/" ? "" : HttpRuntime.AppDomainAppVirtualPath);
}
}
<script type="text/javascript">
var rootPath = '@Html.Raw(GetRootPath())';
</script>
I need to implement the same function in .net core 2.1 . I know it is related to IHostingEnvironment, but in all the examples that I know, they are injecting it in either controllers or in functions of Startup page. How do I implement above function in _Layout.cshtml in .net core 2.1?
Upvotes: 1
Views: 2675
Reputation: 29976
For accessing IHostingEnvironment
in View
, you could try like below:
@using Microsoft.AspNetCore.Http;
@using Microsoft.AspNetCore.Hosting;
@inject IHttpContextAccessor HttpContextAccessor;
@inject IHostingEnvironment HostingEnvironment;
@{
ViewData["Title"] = "About";
}
@HttpContextAccessor.HttpContext.Request.PathBase.Value;
@HostingEnvironment.WebRootPath;
@HostingEnvironment.ContentRootPath;
Upvotes: 1