asp.net-mvc-3 – 从我的控制器调用索引视图时路径中的非法字符

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 从我的控制器调用索引视图时路径中的非法字符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我收到一个ArgumentException,当调用我的控制器之一的索引操作,我不知道为什么。错误消息如下:

‘/’应用程序中的服务器错误

路径中的非法字符。

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

我不知道为什么会发生这种情况。这里是控制器的代码

  1. public ActionResult Index()
  2. {
  3. var glaccounts = db.GLAccounts.ToString();
  4. return View(glaccounts);
  5. }

解决方法

模糊性来自于你使用字符串作为模型类型。这种模糊性可以这样解决
  1. public ActionResult Index()
  2. {
  3. var glaccounts = db.GLAccounts.ToString();
  4. return View((object)glaccounts);
  5. }

要么:

  1. public ActionResult Index()
  2. {
  3. object glaccounts = db.GLAccounts.ToString();
  4. return View(glaccounts);
  5. }

要么:

  1. public ActionResult Index()
  2. {
  3. var glaccounts = db.GLAccounts.ToString();
  4. return View("Index",glaccounts);
  5. }

注意,对象的转换选择正确的方法重载,因为已经有一个View方法,它接受一个表示视图名称的字符串参数,所以你不能把任何你想要的东西=>如果它是一个字符串,它必须是视图的名称,并且此视图必须存在。

猜你在找的asp.Net相关文章