Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - Illegal characters in path when calling the index view from my controller

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);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...