0

I make controller in asp mvc2 app. After that i create view for this controller.

I named this controller "DisplayName" And write something like :

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

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? why i cannot just use name that I want?

Thanx

2 Answers2

0

Yes the default routing in MVC uses convention based routing and the naming convention is to suffix your controller names with "controller".

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
0

Yes, this convention should be followed by default. Here is some formal confirmation from MSDN:

All controller classes must be named by using the "Controller" suffix.

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. And example of that can be found here.

Community
  • 1
  • 1
Andrei
  • 55,890
  • 9
  • 87
  • 108