[英]Must the first view must always be called index.aspx?
我创建了一个名为loginController.cs的控制器,并创建了一个名为login.aspx的视图
如何从loginController.cs调用该视图?
ActionResult
始终设置为索引,为了简洁起见,我想指定控制器在调用时使用的视图,而不是始终调用其默认索引?
希望有道理。
为了真正回答问题,您可以在Global.asax
的默认路由上方添加路由:
routes.MapRoute(
"SpecialLoginRoute",
"login/",
new { controller = "Login", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
..尽管没有正确地考虑要实现的目标(即默认情况下更改MVC的功能),但最终会遇到很多混乱的路线。
您可以自定义MVC路由中的几乎所有内容-路由的外观没有特别的限制(仅顺序很重要),可以用不同于方法名称的名称来命名操作(通过ActionName属性),可以命名所需的视图(例如,通过按名称返回特定视图)。
return View("login");
您可以通过Action方法从控制器返回视图。
public class LoginController:Controller
{
public ActionResult Index()
{
return View();
//this method will return `~/Views/Login/Index.csthml/aspx` file
}
public ActionResult RecoverPassword()
{
return View();
//this method will return `~/Views/Login/RecoverPassword.csthml/aspx` file
}
}
如果您需要返回其他视图(除了操作方法名称,则可以显式提及它)
public ActionResult FakeLogin()
{
return View("Login");
//this method will return `~/Views/Login/Login.csthml/aspx` file
}
如果要返回另一个控制器文件夹中存在的视图,请在〜/ Views中使用完整路径
public ActionResult FakeLogin2()
{
return View("~/Views/Account/Signin");
//this method will return `~/Views/Account/Signin.csthml/aspx` file
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.