4

For my project, I need to create programmatically a MVC solution and project.

I've already search and I've found a way to create a Console Application using Solution2 and EnvDTE80. This is the code :

string commonName = "ConsoleAppProg";
string csPrjPath = $"C:\\TestCreateProject\\{commonName}";
Type type = Type.GetTypeFromProgID("VisualStudio.DTE.15.0");
DTE dte = (DTE)Activator.CreateInstance(type, true);
Solution2 sln = (Solution2)dte.Solution;
sln.Create(csPrjPath, commonName);
sln.SaveAs(commonName + ".sln");
string csProjectTemplatePath = sln.GetProjectTemplate("ConsoleApplication.zip", "CSharp");
sln.AddFromTemplate(csProjectTemplatePath, $"{csPrjPath}\\{commonName}", commonName, false);

But I didn't find a way to create a MVC Project. Searching in the Extensions folder of the VS installation folder, I didn't find a "mvc.zip" or something else except ConsoleApplication and ClassLibrary.

I hope someone can help me with this problem!

Thank you in advance !

2
  • I see directories under C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ProjectTemplates. Did you install any templates? Commented Aug 6, 2018 at 15:50
  • 1
    It would be so easy if you use ASP MVC app in .NET Core. Just use command dotnet new mvc -o ProjectName and that's all. You should consider it :) Commented Aug 6, 2018 at 16:07

1 Answer 1

1

Thanks to Dan Wilson in comments, I've searched in this folder (Reminder : C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ProjectTemplates) and I've found a way to perform a MVC project creation

For those who want to develop it, follow this code :

// PATH
string commonName = "ConsoleAppProg";
string csPrjPath = $"C:\\TestCreateProject\\{commonName}";

// SOLUTION INITIALIZATION
Type type = Type.GetTypeFromProgID("VisualStudio.DTE.15.0");
DTE dte = (DTE)Activator.CreateInstance(type, true);
Solution2 sln = (Solution2)dte.Solution;

string csProjectTemplatePath = sln.GetProjectTemplate("WebApplication.zip|FrameworkVersion=4.5", "CSharp");

// SOLUTION AND PROJECT CREATION
sln.Create(csPrjPath, commonName);
sln.SaveAs(commonName + ".sln");
sln.AddFromTemplate(csProjectTemplatePath, $"{csPrjPath}\\{commonName}", commonName, false);

Look at the first string sln.GetProjectTemplate(). To create a Web Application based on MVC, you must to specify the Framework Version after the pipe inside the string. In my case : 4.5

Thanks again !

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

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.