简体   繁体   English

如何以编程方式创建ASP.NET MVC项目?

[英]How to programmatically create an ASP.NET MVC project?

For my project, I need to create programmatically a MVC solution and project. 对于我的项目,我需要以编程方式创建MVC解决方案和项目。

I've already search and I've found a way to create a Console Application using Solution2 and EnvDTE80. 我已经搜索过了,我找到了一种使用Solution2和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. 但我没有找到创建MVC项目的方法。 Searching in the Extensions folder of the VS installation folder, I didn't find a "mvc.zip" or something else except ConsoleApplication and ClassLibrary. 在VS安装文件夹的Extensions文件夹中搜索,我没有找到“mvc.zip”或除ConsoleApplication和ClassLibrary之外的其他内容。

I hope someone can help me with this problem! 我希望有人可以帮我解决这个问题!

Thank you in advance ! 先感谢您 !

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 感谢Dan Wilson的评论,我在这个文件夹中搜索过(提醒: C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\Common7\\IDE\\ProjectTemplates ),我找到了一种方法来执行MVC项目创建

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() . 查看第一个字符串sln.GetProjectTemplate() To create a Web Application based on MVC, you must to specify the Framework Version after the pipe inside the string. 要基于MVC创建Web应用程序,必须在字符串内部的管道之后指定Framework Version。 In my case : 4.5 就我而言:4.5

Thanks again ! 再次感谢 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM