如何在 WebForms 项目中启用 MVC?

How to enable MVC in a WebForms project?(如何在 WebForms 项目中启用 MVC?)
本文介绍了如何在 WebForms 项目中启用 MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的网络表单项目.我从 Nuget 添加了 MVC5,并添加了以下内容:

I have an existing webforms project. I've added MVC5 from Nuget, and added the following:

_Viewstart.cshtml
Shared
   _Layout.cshtml
Areas
   Test
      Controllers
        TestController.cs
      Views
        Test.cshtml

我的控制器:

     public class TestController : Controller
    {
    [Route("Test/Test")]
    public ActionResult Test()
    {
        return View();
    }
    }

我的Global.asax:

        AreaRegistration.RegisterAllAreas();
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        RouteConfig.RegisterRoutes(RouteTable.Routes); 

然而,当我导航到 /Test/Test 时,我得到 404 not found.我错过了什么?

Yet when I navigate to /Test/Test, I get 404 not found. What am I missing?

更新我最终基于模板化的 MVC5 项目向我的 web.config 添加了一些内容,现在我已经命中了控制器方法并定位了视图.我现在正在处理其他事情,但我相信它与 MVC 配置无关,所以我认为它真的那么容易".

UPDATE I ended up adding a few things to my web.config based on the templated MVC5 project and I now have the controller method being hit and the view being located. I'm dealing with something else now but I believe it's unrelated to MVC configuration, so I think it really is "that easy".

推荐答案

在查看默认的 MVC5 web.config 后,将以下内容添加到我的配置中让我启动并运行:

After looking at the default MVC5 web.config, adding the following to my config got me up and running:

    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

还有:

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
  </dependentAssembly>

这篇关于如何在 WebForms 项目中启用 MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)