Question

I have an asp.net mvc project that uses some search methods in a seperate library. This library needs to know the location of my lucene index files.

    private static string lucenePath = ConfigurationManager.AppSettings["lucenePath"];

    public static ColorList SearchColors(Query query) {
        return new ColorList(
            new IndexSearcher(Path.GetFullPath(lucenePath)),
            query);
    }

This correctly reads my configured lucenePath from the web.config's application key node. But how can I get the correct full path from this relative path? Path.GetFullPath gives me a completely incorrect path.

--Conclusion--
If you want to go full-out, tvanfosson's answer is probably for you.
I, however, kept it a little more brain dead by using the following:

Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
    ConfigurationManager.AppSettings["luceneIndex"].TrimStart('\\'));

This will look in the caller's app.config for an appkey called "path" and combine its value to the caller's path. The TrimStart() makes sure that the config file can both contain a leading \ or not.

Was it helpful?

Solution

Since you are referencing this from a separate library, you might have to jump through a bunch of hoops to get access to the HttpServerUtitity or introduce some coupling to classes that are difficult to mock. You might want to consider having a single configuration class that loads properties from the web configuration that gets injected into your library via constructor/setter. To make it easier to test against, you could define an interface that could be mocked in your unit tests and have it implement that. The configuration class could use the HttpServerUtility to obtain the absolute path and store it internally to be reused.

OTHER TIPS

Server.MapPath(string);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top