简体   繁体   English

显示MVC2视图

[英]Displaying mvc2 view

I make controller in asp mvc2 app. 我在ASP MVC2应用中制作控制器。 After that i create view for this controller. 之后,我为此控制器创建视图。

I named this controller "DisplayName" And write something like : 我将此控制器命名为“ 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);
    }
}

But when I try to display it in web browser and call: 但是,当我尝试在网络浏览器中显示它并调用时:

http://localhost:xxxxx/DisplayName/DisplaySomething

I get error : 我收到错误消息:

Server Error in '/' Application. The resource cannot be found.

I trying to find error and then i see one sample and rename controller in DisplayNameController 我尝试查找错误,然后看到一个示例并在DisplayNameController中重命名控制器

Now i have: 我现在有:

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);
    }
}

And now when i call: 现在,当我打电话给我时:

http://localhost:xxxxx/DisplayName/DisplaySomething

app work perfect. 应用工作完美。

My question is next: Is this mean that every controller need to have "Controller" in name? 接下来的问题是:这意味着每个控制器都需要名称中的“ Controller”吗? why i cannot just use name that I want? 为什么我不能只使用我想要的名字?

Thanx 谢谢

是的,MVC中的默认路由使用基于约定的路由,命名约定是在控制器名称后加上“ controller”。

Yes, this convention should be followed by default. 是的,默认情况下应遵循此约定。 Here is some formal confirmation from MSDN : 这是MSDN的一些正式确认:

All controller classes must be named by using the "Controller" suffix. 必须使用“ Controller”后缀来命名所有控制器类。

However you can, if you really want to, redefine this naming convention. 但是,如果您确实愿意,可以重新定义此命名约定。 For this you have to create your own ControllerFactory class, which is responsible for controller instantiation. 为此,您必须创建自己的ControllerFactory类,该类负责控制器实例化。 And example of that can be found here . 可以在这里找到示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM