shubhkr1
shubhkr1

Reputation: 98

HttpRuntime.AppDomainAppVirtualPath equivalent in .net core 2.1

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

Answers (1)

Edward
Edward

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

Related Questions