[英]Displaying mvc2 view
我在ASP MVC2应用中制作控制器。 之后,我为此控制器创建视图。
我将此控制器命名为“ DisplayName”,并编写如下内容:
public class DisplayName : Controller
{
//
// GET: /DisplayName/
public ActionResult Index()
{
return View();
}
public ActionResult DisplaySomething()
{
MojaKlasa objCustomer = new MojaKlasa();
objCustomer.ime = "Random ime";
objCustomer.broj = 10;
return View("DisplaySomething", objCustomer);
}
}
但是,当我尝试在网络浏览器中显示它并调用时:
http://localhost:xxxxx/DisplayName/DisplaySomething
我收到错误消息:
Server Error in '/' Application. The resource cannot be found.
我尝试查找错误,然后看到一个示例并在DisplayNameController中重命名控制器
我现在有:
public class DisplayNameController : Controller
{
//
// GET: /DisplayName/
public ActionResult Index()
{
return View();
}
public ActionResult DisplaySomething()
{
MojaKlasa objCustomer = new MojaKlasa();
objCustomer.ime = "Random ime";
objCustomer.broj = 10;
return View("DisplaySomething", objCustomer);
}
}
现在,当我打电话给我时:
http://localhost:xxxxx/DisplayName/DisplaySomething
应用工作完美。
接下来的问题是:这意味着每个控制器都需要名称中的“ Controller”吗? 为什么我不能只使用我想要的名字?
谢谢
是的,MVC中的默认路由使用基于约定的路由,命名约定是在控制器名称后加上“ controller”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.