webApi 相对路径

因为 WebApi  用到了一张图片资源,

设置 图片为 始终 复制。

System.Web.Hosting.HostingEnvironment.MapPath($"/SecurityCard/card.png")

        public static string GetCardPath()
        {
            //FileInfo card = new FileInfo(@"D:\_SVN\project\Visual Studio 2017\WebApiProject\WebSecurity\WebSecurity\SecurityCard\card.png");
            FileInfo card = new FileInfo(System.Web.Hosting.HostingEnvironment.MapPath($"/SecurityCard/card.png"));
            if (card.Exists)
            {
                return card.FullName;
            }
            else
            {
                throw  new System.Exception("找不到图片");
            }           
        }

然后就可以在代码中 随性的使用了, 不管它发布到哪里 都可以。

猜你喜欢

转载自blog.csdn.net/nicepainkiller/article/details/86625933