2

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

This looks like a really cool solution. I did what you said (and copied template files into my project like Hanselman says in his blog post). Visual studio did some kind of an upgrade after reloading the project. That was expected I guess. Now I can open the "Add Controller" dialog but it gives an error when trying to generate. <<< Unable to retrieve metadata for 'DemoWebApiConsole.Company'. No connection string named 'EmployeeContext' could be found in the application config file. >>> Believe me, EmployeeContext does exist in app.config file.
What kind of controller/ template are you trying to build from? ( standard EF one? Bit of a long shot but perhaps worth adding a dummy web.config to see if the templates are bent towards looking for web stuff not app.config.
Somehow closing and restarting Visual Studio did the trick. Now it does see the Context but can not generate controller saying <<<Context type not supported>>> I am using a MySql database. I guess that is the problem. Anyways ... good solution. Thanks...
Glad it helped, if not a complete fix. I just went through the steps myself including the EF scaffolding. It worked but I was creating a <New Data Context...>.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.