Andrew Shepherd
Andrew Shepherd

Reputation: 45262

Adding asp.net.mvc to ASP.NET: Controllers folder

I have an ASP.NET application, and I am trying to turn this into a hybrid ASP.NET / ASP.NET.MVC 4.0 application.

I tried to create a folder named "Controllers", and place a .cs file in there:

public class PlayerGroupController : Controller
{
    public PlayerGroupController()
    {

    }

    public string Index()
    {
        return "Hello World!";
    }

    public ActionResult LayoutTemplates()
    {
        return View();
    }
}

Any attempt to access "PlayerGroup/LayoutTemplates" doesn't work (just get a "Not Found" error)

I then moved this file into App_Code, and it works fine. I'm glad I got something working, but I would rather follow the convention of controller classes being in the folder named Controllers.

Is there some magic setting I can set somewhere so that it starts recognizing Controllers as a code folder?

Upvotes: 3

Views: 157

Answers (1)

Eilon
Eilon

Reputation: 25704

If the project is configured as a Web Site Project then all code files (*.cs) must be in ~/App_Code or in subfolders thereof. If the project is a Web Application Project, code files can go anywhere in the project and be compiled by VS into a DLL that ends up in the ~/bin directory, which then gets loaded by ASP.NET.

MVC is geared specifically towards the Web Application Project (WAP), so I recommend creating a new WAP and copying all the files into that, and then going from there.

Upvotes: 5

Related Questions