[英]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.