【问题标题】:How can I get the "Add -> View" menu item when adding ASP.NET MVC pages to a Web Forms project?将 ASP.NET MVC 页面添加到 Web 窗体项目时,如何获取“添加 -> 查看”菜单项?
【发布时间】:2009-08-31 22:59:03
【问题描述】:

我正在向现有的 ASP.NET Web 窗体项目添加一些 ASP.NET MVC 页面。

我已经能够从我创建的 MVC 项目中移植一些模型、视图和控制器,并且它们运行良好。

但我想在我的项目中添加一些新的“强类型”视图,但我的 Web 窗体项目中没有新视图向导。

在自定义 Visual Studio 方面我还是个新手,所以我可能遗漏了一些明显的东西。

【问题讨论】:

    标签: asp.net-mvc visual-studio-2008 webforms


    【解决方案1】:

    您可以对项目文件进行一些小技巧。在您的 WebForms 项目文件中(将其作为普通文件打开)在 ProjectTypesGuid 节点下添加以下 guid

    {603c0e0b-db56-11dc-be95-000d561079b0};

    然后添加对 System.Web.Routing、Abstractions 和 MVC 的引用,您应该一切顺利......

    基本上是这个过程的逆过程...

    http://weblogs.asp.net/leftslipper/archive/2009/01/20/opening-an-asp-net-mvc-project-without-having-asp-net-mvc-installed-the-project-type-is-not-supported-by-this-installation.aspx

    【讨论】:

      【解决方案2】:

      我刚刚在 CodeProject 博客上找到了答案:

      http://www.codeproject.com/KB/aspnet/webformmvcharmony.aspx?msg=3161863#heading0009

      它涉及手动编辑 .csproj 文件并将 guid 添加到项目类型列表中。

      【讨论】:

        【解决方案3】:

        就像 John Foster 所说,您必须手动编辑 .csproj 文件。但是,我需要一个不同的 guid 和一些额外的元素(至少对于 Visual Studio 2012),否则我在 Add->View 之后得到一个错误对话框,上面写着“参数不能为空。参数名称 path1”。这是我所做的:

        1. 右键项目->“卸载项目”
        2. 右键项目->“编辑.csproj”
        3. 添加 {E3E379DF-F4C6-4180-9B81-6769533ABE47};到 Project\PropertyGroup\ProjectTypeGuids。我的看起来像:

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

        4. 确保 Project\PropertyGroup 中存在以下元素:

        【讨论】:

          【解决方案4】:

          新的视图向导是 ASP.Net MVC 脚手架的一部分。您的项目需要从 ASP.Net MVC 项目模板创建才能获得此功能。

          由于您要将其添加到旧的 ASP.Net 项目中,可能是使用 Web 应用程序或网站项目模板创建的,因此您必须手动完成。

          【讨论】:

            【解决方案5】:

            当您的控制器方法的返回结果为 void 而不是 ActionResult

            时,我也看到了这种情况
               //Right click and the context menu will NOT show "Add View"
               public void Details(int id)
               {
                  Dinner dinner = dinnerRepository.GetDinner(id);
                  if (dinner == null)
                     return View("NotFound");
                  else
                     return View("Details", dinner);
               }
            
               //Right click and the context menu will show "Add View"
               public ActionResult Details(int id)
               {
                  Dinner dinner = dinnerRepository.GetDinner(id);
                  if (dinner == null)
                     return View("NotFound");
                  else
                     return View("Details", dinner);
               }
            

            【讨论】:

              猜你喜欢
              • 2023-04-11
              • 1970-01-01
              • 1970-01-01
              • 2017-11-29
              • 1970-01-01
              • 2013-11-07
              • 1970-01-01
              • 1970-01-01
              • 2016-04-19
              相关资源
              最近更新 更多