我在Visual Studio中创建了一个Web API项目.我正在使用属性路由.这是Controllers文件夹下的控制器:
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
我不明白为什么它在那里时找不到视图,或者为什么它甚至试图用查询字符串参数的名称来找到视图.
解决方法:
它可能可以找到视图.它是找不到的母版页.
那是因为您正在使用
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.
提示是在搜索视图中包含参数值.因为您传递的platform参数是一个字符串,所以它与使用字符串viewName和string masterName参数的方法相匹配.
控制器的ViewResult View()方法有很多过载.在这种情况下,您可能希望将平台作为对象模型传递.您可以通过使用命名参数来解决此问题,这可以通过让编译器知道要调用的重载方法来避免混淆.
public class RegistrationController : Controller {
// GET: Registration
[Route("")]
public ActionResult CreateUser(string platform) {
return View("~/Views/Registration/CreateUser.cshtml", model: platform);
}
}
从那里一切都应该按预期工作