0

I have a solution which has two presentation layers. One is using ASP.NET MVC, the other WinForms. Both projects depend on a third projecs which does some business logic and is a class library. The third project has the following method which works fine in WinForms, but doesnt work in MVC:

    public static string[] Dictionary()
    {
        if (_dictionary == null)
        {
            if (File.Exists("dict.txt"))
                _dictionary = File.ReadAllLines("dict.txt");
        }

        return _dictionary;
    }

So when I am using the WinForms GUI the file is found, but when I am using the MVC website, the project can't find it.

When building the solution dict.txt is placed in bin/debug of the WinForms project.

It is placed in /bin in the MVC project.

Where should I place the file in the MVC proejct?

Kenci
  • 4,435
  • 12
  • 60
  • 98

3 Answers3

2

You can place them in the App_Data Forlder, and then reference them like this

 var fileName = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(@"~/App_Data"),"dict.txt");
dariogriffo
  • 3,751
  • 1
  • 15
  • 31
  • I edited my question, forgot to tell that the project which uses Dictionary() is a class library and does not have System.Web.HttpContext.Current.Server.MapPath – Kenci Dec 23 '14 at 14:59
  • change the method to receive the full path instead of resolving in the method public static string[] Dictionary(fileName), in that way you resolve the path in both apps and the method then has the logic – dariogriffo Dec 23 '14 at 15:00
  • Is it not possible to let the class library use it's own version of the dict.txt file, or does it also have to be in the MVC project? – Kenci Dec 23 '14 at 15:01
  • You can user resource files http://msdn.microsoft.com/en-us/library/7k989cfy(VS.80).aspx – dariogriffo Dec 23 '14 at 15:04
1

So when I am using the WinForms GUI the file is found, but when I am using the MVC website, the project can't find it.

To answer the reason why you are observing the difference is called the current working directory. By default, when VS.NET launches Windows apps or consoles, the working directory is to the relevant bin directory.

All relative file operations, such as File.ReadAllLines("the.file"), will concatenate to an absolute file path as per below.

Path.Combine(Directory.GetCurrentDirectory(), "the.file");

ASP.NET's working directory would not correlate to the root of your web application.

Where should I place the file in the MVC proejct?

As suggested by @dariogriffo, it should be placed in the ~/App_Data/* directory.

I have a solution which has two presentation layers. One is using ASP.NET MVC, the other WinForms. Both projects depend on a third projecs which does some business logic and is a class library.

So now you are in a little bit of a pickle over here with your current implementation and the way the current working directory is implemented. Without delving into too much detail, I suggest that the code figures out how to load the file indirectly such as passing in an interface with a contract such as the following.

public interface IDictionaryRepository {
    IEnumerable<string> LoadAllWords();
}

With the above, you can change your business logic to the following

public static string[] Dictionary()
{
    if (_dictionary == null)
    {
        _dictionary = _dictionaryRepository.LoadAllWords().ToArray();
    }

    return _dictionary;
}

The benefit of the above abstraction is that you may then easily substitute the file finding logic as per the other suggestions easily, and maybe migrate your dictionary to be hosted in a database somewhere.

justin.lovell
  • 679
  • 3
  • 11
0

server applications can have 2 paths - virtual and logical. You can reference to one or another using MapPath() method.

Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?

I thinki you need to provide full path to this file in MVC, check this out:

Server.MapPath("~\\bin\\dict.txt")
Community
  • 1
  • 1
WholeLifeLearner
  • 395
  • 2
  • 19
  • The Dictionary() method is in a class library which is used by the MVC project. Can I use Server.MapPath there? – Kenci Dec 23 '14 at 15:00