2016-07-20 32 views
1

eine Hilfsmethode wie dieses zu erhalten, mit:wie Server.MapPath in asp.net Kern aus dem Inneren eines Helfer

public static IHtmlContent Source(this IHtmlHelper html, string s) 
{ 
    var path = ServerMapPath() + "Views\\" + s; 

brauche ich

das Äquivalent von Server.MapPath in asp.net Kern erhalten
+0

Obwohl spät, siehe meine Antwort auf eine ähnliche Frage: https://stackoverflow.com/questions/30656089/reading-a-file-in-mvc-6/35459834#35459834. –

Antwort

5

Sie müssen IHostingEnvironment injizieren. Dann:

var path = env.ContentRootPath + "Views\\" + s;

in einer HTML-Helfer können Sie dies tun:

((IHostingEnvironment)html.ViewContext.HttpContext.RequestServices.GetService(typeof(IHostingEnvironment))).ContentRootPath; 
+0

gefunden wie folgt: '((IHostingEnvironment) html.ViewContext.HttpContext.RequestServices.GetService (Typof (IHostingEnvironment))). ContentRootPath' – Omu

+0

Besser,' Path.Combine' und nicht Verkettung von Strings zu verwenden –

0

für eine komplette Lösung (MapPath(), IsPathMapped(), UnmapPath()) buchen, meine Antwort auf diese Frage: Reading a file in MVC 6.

Verwandte Themen