错误如:未找到视图"LoginRegister"或其主控或没有视图引擎支持搜索的位置.搜索了以下位置:
〜/查看/我的帐户/ LoginRegister.aspx
〜/查看/我的帐户/ LoginRegister.ascx
〜/查看/共享/ LoginRegister.aspx
〜/查看/共享/ LoginRegister.ascx
〜/查看/我的帐户/ LoginRegister.cshtml
〜/查看/我的帐户/ LoginRegister.vbhtml
〜/查看/共享/ LoginRegister.cshtml
〜/查看/共享/ LoginRegister.vbhtml
实际上我的页面浏览页面就是 ~/Views/home/LoginRegister.cshtml我所做的
而我的RouteConfig是
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "MyAccount", action = "LoginRegister", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
Nig*_*ist 98
如果您的模型类型是String,请小心,因为View的第二个参数(字符串,字符串)是masterName,而不是model.您可能需要使用object(model)作为第二个参数调用重载:
不正确 :
protected ActionResult ShowMessageResult(string msg)
{
return View("Message",msg);
}
Run Code Online (Sandbox Code Playgroud)
正确:
protected ActionResult ShowMessageResult(string msg)
{
return View("Message",(object)msg);
}
Run Code Online (Sandbox Code Playgroud)
或(由bradlis7提供):
protected ActionResult ShowMessageResult(string msg)
{
return View("Message",model:msg);
}
Run Code Online (Sandbox Code Playgroud)
Cla*_*ies 13
在Microsoft ASP.net MVC中,用于解析传入和传出URL组合的路由引擎的设计理念是"约定优于配置".这意味着如果您遵循路由引擎使用的约定(规则),则不必更改配置.
ASP.net MVC的路由引擎不提供网页(.cshtml).它为您的代码中的类处理URL提供了一种方法,可以将text/html呈现给输出流,或者使用Convention以一致的方式解析和提供.cshtml文件.
用于路由的约定是将Controller与名称类似的类匹配,ControllerNameController即controller="MyAccount"表示查找类名MyAccountController.接下来是动作,它被映射到Controller类中的一个函数,该函数通常返回一个ActionResult.即action="LoginRegister"会寻找一个功能public ActionResult LoginRegister(){}控制器的类.此函数可能会返回一个View()名为Convention LoginRegister.cshtml的/Views/MyAccount/文件,并将存储在该文件夹中.
总而言之,您将拥有以下代码:
/Controllers/MyAccountController.cs:
public class MyAccountController : Controller
{
public ActionResult LoginRegister()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
/Views/MyAccount/LoginRegister.cshtml:您的视图文件.
在返回视图的LoginRegister操作中,执行以下操作,我知道这可以在mvc 5中完成,我不确定是否在mvc 4中也是如此.
public ActionResult Index()
{
return View("~/Views/home/LoginRegister.cshtml");
}
Run Code Online (Sandbox Code Playgroud)
检查您的视图(.cshtml 文件)的构建操作它应该设置为内容。在某些情况下,我已经看到构建操作被设置为 None(错误地)并且这个特定的视图没有部署在目标机器上,即使您看到该视图存在于有效文件夹下的 Visual Studio 项目文件中
| 归档时间: |
|
| 查看次数: |
146529 次 |
| 最近记录: |