我在 Visual Studio 中创建了一个 Web API 项目。我正在使用属性路由。这是 Controller 文件夹下的 Controller :
public class RegistrationController : Controller
{
// GET: Registration
[Route("")]
public ActionResult CreateUser(string platform)
{
return View("~/Views/Registration/CreateUser.cshtml", platform);
}
}
当我通过 URL http://localhost/application
调用 CreateUser
操作时它有效,但是当我尝试通过 URL 传递查询字符串参数时>http://localhost/application?platform=android
,它给出了以下错误:
The view '~/Views/Registration/CreateUser.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Registration/CreateUser.cshtml
~/Views/Registration/android.master
~/Views/Shared/android.master
~/Views/Registration/android.cshtml
~/Views/Registration/android.vbhtml
~/Views/Shared/android.cshtml
~/Views/Shared/android.vbhtml
我不明白为什么它找不到 View ,或者为什么它甚至试图找到具有查询字符串参数名称的 View 。
最佳答案
大概能找到view。这是它找不到的母版页。
那是因为你正在使用
class Controller : ... {
ViewResult View(string viewName, string masterName);
}
重载方法,其中
Creates a System.Web.Mvc.ViewResult object using the view name and master-page name that renders a view to the response.
线索是它在搜索 View 时包含了参数值。因为您传递的平台参数是一个字符串,所以它匹配调用到使用 string viewName
和 string masterName
参数的方法。
Controller
对 ViewResult View()
方法有很多过载。在这种情况下,您可能希望将 platform
作为对象模型传递。您可以通过使用命名参数来解决此问题,这将通过让编译器知道您打算调用哪个重载方法来避免混淆....
public class RegistrationController : Controller {
// GET: Registration
[Route("")]
public ActionResult CreateUser(string platform) {
return View("~/Views/Registration/CreateUser.cshtml", model: platform);
}
}
从那里一切都应该按预期工作
关于c# - 找不到 ASP.NET View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38675848/