WebForms and ASP.NET MVC co-existence

心不动则不痛 提交于 2020-01-11 09:16:18

问题


I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I've done to make that happen is that I added a namespaces node to the WebForms web.config:

<pages styleSheetTheme="Default">
  ...
  <namespaces>
    <add namespace="System.Web.Mvc"/>
    <add namespace="System.Web.Mvc.Ajax"/>
    <add namespace="System.Web.Mvc.Html"/>
    <add namespace="System.Web.Routing"/>
  </namespaces>      
</pages>

However, when I try to start the project, I get an error stating: "Compiler Error Message: CS0234: The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)"

I have System.Web.Mvc referenced. What is the issue?


回答1:


Add System.Web.Mvc in the compilation section,

<compilation debug="true">
    <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </assemblies>
</compilation>



回答2:


It's easier to start with an MVC project, and then start adding WebForms pages.

For your error, you will need to add references for the following assemblies:

  • System.Web.Mvc
  • System.Web.Routing
  • System.Web.Abstractions

There will be much more to add in the web.config to get MVC working correctly. To get it all, I'd suggest creating an MVC project, and merging the web.config files.



来源:https://stackoverflow.com/questions/1727309/webforms-and-asp-net-mvc-co-existence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!