.net core 入坑经验 - 2、MVC Core之获取网站运行路径

这次是建立了asp.net mvc core项目,在controller中想获取网站在硬盘中的路径,找了一圈Server.MapPath() 已不存在,HttpContent也一样,经过查阅资料发现是如下方法来获取路径

将Controller增加构造方法,传入的参数为IHostingEnvironment对象实例。

而在Controller中的方法中可以通过IHostingEnvironment对象的WebRootPath属性获取到路径

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;

namespace WebApplication1.Controllers
{
    public class UserController : Controller
    {
        private IHostingEnvironment host = null;

        public UserController(IHostingEnvironment host)
        {
            this.host = host;
        }

        public IActionResult Login()
        {
            //host.WebRootPath
            return View();
        }
    }
}

 

posted @ 2017-05-04 22:47  jgjg2323  阅读(5666)  评论(1编辑  收藏  举报