30

I am receiving an ArgumentException when invoking the index action of one of my controllers and I am not sure why. The error message is the following:

Server Error in '/' Application.

Illegal characters in path.

[ArgumentException: Illegal characters in path.]
 System.IO.Path.CheckInvalidPathChars(String path) +126
 System.IO.Path.Combine(String path1, String path2) +38

I am not sure why this is happening. here is the code from the controller:

    public ActionResult Index()
    {
        var glaccounts = db.GLAccounts.ToString();
        return View(glaccounts);
    }
gyurisc
  • 11,234
  • 16
  • 68
  • 102

2 Answers2

64

The ambiguity comes from the fact that you are using string as model type. This ambiguity could be resolved like this:

public ActionResult Index()
{
    var glaccounts = db.GLAccounts.ToString();
    return View((object)glaccounts);
}

or:

public ActionResult Index()
{
    object glaccounts = db.GLAccounts.ToString();
    return View(glaccounts);
}

or:

public ActionResult Index()
{
    var glaccounts = db.GLAccounts.ToString();
    return View("Index", glaccounts);
}

Notice the cast to object to pick the proper method overload as there is already a View method which takes a string argument which represents the view name so you cannot throw whatever you want to it => if it's a string it must be the name of the view and this view must exist.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks for the answer. I should have posted this question instead of spending a couple of hours of dismantling my MVC project and adding MVC source code. Thanks a lot! – gyurisc Apr 12 '11 at 13:01
  • This is one of the most ridiculous things I've found in ASP.NET MVC. Why we can't pass a string as the model to a view? – Saeed Neamati Nov 14 '11 at 03:14
  • 1
    @SaeedNeamati, this has nothing to do with ASP.NET MVC. It is the way method overloading works in .NET in general. – Darin Dimitrov Nov 14 '11 at 06:50
  • The `View()` method takes an object as its model. So, technically, because all object in .NET are inherited directly or indirectly from `object` class, you can pass a string as the model. Then, what is has to do with the method overloading? Besides, this error means that a bad request has been sent to the server, a bad URL. Maybe ASP.NET MVC, sends an internal request (something like URL rewriting) to get the view. – Saeed Neamati Nov 14 '11 at 07:29
1

I found it finally. It is a really embarassing typo by me. I mistyped the code:

    public ActionResult Index()
    {
        var glaccounts = db.GLAccounts.ToString();
        return View(glaccounts);
    }

instead of:

    public ActionResult Index()
    {
        var glaccounts = db.GLAccounts.ToList();
        return View(glaccounts);
    }

Then the framework wanted to load a view file, like this:

"~/Views/GLAccount/SELECT \r\n[Extent1].[Id] AS [Id], \r\n[Extent1].[OrgDefinitionId] AS [OrgDefinitionId], \r\n[Extent1].[GLAccountId] AS 
[GLAccountId], \r\n[Extent1].[Name] AS [Name], \r\n[Extent1].[StartDate] AS [StartDate], 
\r\n[Extent1].[EndDate] AS [EndDate]\r\nFROM [GLAccounts] AS [Extent1].aspx"

Hopefully, I will save couple of hours of debugging for someone else by posting this :(

gyurisc
  • 11,234
  • 16
  • 68
  • 102