e-mre
e-mre

Reputation: 3383

Generating controllers in a ASP.NET Web API Self-Host application

ASP.NET Web API project template in Visual Studio 2012 is a specialized MVC4 project and after creating an instance of such a project, right clicking on the "Controllers" folder shows you "Add >> Controller" menu item which triggers the controller generator. (scaffolding)

On the other hand, it is also possible to host an ASP.NET Web API server in a console application, which is called in the documentation a Self-Host application.

http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api

My question is: How can I use the "Controller Scaffolding" if I am developing a self hosting console application?

Upvotes: 2

Views: 1826

Answers (2)

Mark Jones
Mark Jones

Reputation: 12194

Scaffolding is still supported via the package manager console e.g. here.

But the MVC specific "Add Controller" dialog is indeed reserved for MVC project files.

You can however, force your self host project to include this dialog.

This can be achieved by adding a line into the project file to "trick" visual studio into thinking your self host app is a MVC app. See this question for details "Add Controller" / "Add View" in a hybrid MVC/WebForms ASP.NET application

If you:

  1. Create a Controllers folder under your project root
  2. Right mouse button your self host project file and "Unload Project"
  3. Right mouse button your self host project file "Edit "
  4. Under the Xml > Project -> PropertyGroup Add the node shown under the steps
  5. Save
  6. Right mouse button your self host project file click reload project

    <ProjectTypeGuids> 
        {E3E379DF-F4C6-4180-9B81-6769533ABE47};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}
    </ProjectTypeGuids>
    

Once you have done this you can start playing around with TT files (scaffolds) to make them more useful/specific to self hosting http://www.hanselman.com/blog/ModifyingTheDefaultCodeGenerationscaffoldingTemplatesInASPNETMVC.aspx

Upvotes: 1

Mark Berryman
Mark Berryman

Reputation: 929

I don't think this is currently possible. This scaffolding support is specific to MVC 4 projects currently.

Upvotes: 0

Related Questions