我如何才能找到从控制器构造函数,ASP.NET MVC中的文件夹路径? [英] How Can I Find The Path To A Folder from a Controller Constructor in ASP.NET MVC?

查看:228
本文介绍了我如何才能找到从控制器构造函数,ASP.NET MVC中的文件夹路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的文件夹路径在我的网站的根,并将其保存到时候我的控制器调用构造函数的类属性:

I am trying to get the path to a folder in my website root and save it to a class property when my controller constructor is called:

public TestController:Controller{
    string temp;

    public TestController(){
        temp = "";
        }

    }

我已经试过如下:

I have tried the following:

temp = Server.MapPath("~/TheFolder/"); // Server is null - error.
temp = Request.PhysicalApplicationPath + @"TheFolder\"; // Request is null - error.

任何想法?

推荐答案

AppDomain.CurrentDomain.BaseDirectory会给你的网站的根目录。所以:

AppDomain.CurrentDomain.BaseDirectory will give you the root of your site. So:

temp = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TheFolder");

(更新感谢马克Gravell的评论)

(Update thanks to Marc Gravell's comment)

这篇关于我如何才能找到从控制器构造函数,ASP.NET MVC中的文件夹路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆