How Can I Find The Path To A Folder from a Controller Constructor in ASP.NET MVC?

后端 未结 3 890
野的像风
野的像风 2021-02-13 15:30

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:Contro         


        
3条回答
  •  孤街浪徒
    2021-02-13 15:43

    Do you actually need this path during the constructor? If you don't need it until the main page cycle begins, consider deferring it - just using a regular property; something like

    public string BasePath {
        get { return Server.MapPath("~/TheFolder/"); }
    }
    

    Then when this is used during the page cycle, it should be fine. You could cache it if you really want to, but I don't imagine this is going to be a bottleneck:

    private string basePath;
    public string BasePath {
        get {
            if(basePath == null) basePath = Server.MapPath("~/TheFolder/");
            return basePath;
        }
    }
    

提交回复
热议问题