一个从asp.net mvc 3升级到asp.net mvc 4的项目发生了如下错误:

[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from ‘System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ in the context ‘Default’ at location ‘C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll’. Type B originates from ‘System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ in the context ‘Default’ at location ‘C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\7c2060f6\6145b34e\assembly\dl3\9036b7c7\fc8023c0_73fecb01\System.Web.WebPages.Razor.DLL’.

这是因为本地程序集和GAC的程序集冲突了, 是因为在Web Pages
1.0中他在web.config内新增了一个配置节点。Web Pages
1.0在Web.config中目前我已发现到新增了一个配置节点,路径在~/Views/web.config.而不是web.config.

<configuration>

   <system.web.webPages.razor />
<!–注意他于常用的system.web同级—>

   <system.web />

</configuration>

修改~/Views/web.config成如下:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>

    <!--
        Enabling request validation in view pages would cause validation to occur
        after the input has already been processed by the controller. By default
        MVC performs request validation before a controller processes the input.
        To change this behavior apply the ValidateInputAttribute to a
        controller or action.
    -->
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

接着修改~/web.config文件

<system.web>
  <compilation debug=”true” targetFramework=”4.0″>
    <assemblies>
      <add assembly=”System.Web.Abstractions, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35″ />
      <add assembly=”System.Web.Helpers, Version=2.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=4.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35″ />
      <add assembly=”System.Web.WebPages, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35″ />
    </assemblies>
  </compilation>
  <authentication mode=”Forms”></authentication>
  <pages>
    <namespaces>
      <add namespace=”System.Web.Helpers” />
      <add namespace=”System.Web.Mvc” />
      <add namespace=”System.Web.Mvc.Ajax” />
      <add namespace=”System.Web.Mvc.Html” />
      <add namespace=”System.Web.Routing” />
      <add namespace=”System.Web.WebPages” />
    </namespaces>
  </pages>
</system.web>

具体也可以参照 ASP.NET MVC3 系列教程 –
如何使项目Debug进MVC3源代码

在同一应用中混合使用ASP.NET窗体和ASP.NET
MVC

不是所有的ASP.NET
MVC Web应用程序都需要从头创建,也许您希望将现有的ASP.NET应用移植到ASP.NET
MVC。在同一个应用同时使用ASP.NE窗体和ASP.NET
MVC可能吗?答案是-完全可以。

在同一个应用同时使用ASP.NE窗体和ASP.NET
MVC不仅是可行的,而且是相当容易的,因为ASP.NET
MVC是在ASP.NET基础上构建的框架。实际上它们仅有一个关键处不同:ASP.NET是在System.Web名称空间下,而ASP.NET
MVC在System.WebSystem.Web.RoutingSystem.Web.Abstractions,
以及System.Web.Mvc四个名称空间下,这意味着,在现有ASP.NET应用程序中添加对这些库(assemblies)的引用,是组合应用这两种技术的起步。

ASP.NET
MVC在ASP.NET基础上构建的另外一个优点是:应用数据能够很容易地在两种技术间共享。例如,Session这个状态对象在两种技术下都可以使用,这样使得通过Session状态来高效地共享数据。

一个ASP.NET窗体应用程序可以通过执行以下步骤,成为一个ASP.NET
MVC应用程序。

1.   在现有的ASP.NET应用程序中,添加以下库的引用:System.Web.Routing.dllSystem.Web.Abstractions.dll,
andSystem.Web.Mvc.dll**;

2.   在现有ASP.NET应用程序中添加Controllers,Views,Views/Shared文件夹;

3.   修改web.config文件,如下(注意是修改ASP.NET应用程序现有的web.config,而不是覆盖掉):

<
?xml version=”1.0″?>
<configuration>
 <system.web>
   <compilation
debug=”false”>
     <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″/>
     </assemblies>
   </compilation>
   <pages>
     <namespaces>
       <add
namespace=”System.Web.Mvc”/>
       <add
namespace=”System.Web.Mvc.Ajax”/>
       <add
namespace=”System.Web.Mvc.Html” />
       <add
namespace=”System.Web.Routing”/>
       <add
namespace=”System.Linq”/>
       <add
namespace=”System.Collections.Generic”/>
     </namespaces>
   </pages>
   <httpModules>
     <add
name=”UrlRoutingModule”
          type=”System.Web.Routing.UrlRoutingModule,
                System.Web.Routing,
Version=3.5.0.0,
                Culture=neutral,
PublicKeyToken=31BF3856AD364E35″ />
   </httpModules>
 </system.web>
</configuration>

4.   配置路由。往Global.asax中加入默认的ASP.NET
MVC全局应用程序类(Global
Application Class)。

using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MixingBothWorldsExample
{
   public
class Global : System.Web.HttpApplication
   {
       public
static void RegisterRoutes(RouteCollection routes)
       {
           routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
           routes.IgnoreRoute(“{resource}.aspx/{*pathInfo}”);
           routes.MapRoute(
              “Default”,
// Route name
              “{controller}/{action}/{id}”,
// URL with parameters
              new
{ controller = “Home”, action = “Index”, id = “” }
// Parameter defaults
               );
       }
       protected
void Application_Start()
       {
           RegisterRoutes(RouteTable.Routes);
       }
   }
}

请注意以下一行代码,它防止ASP.NET窗体的请求被路由到ASP.NET
MVC中:

routes.IgnoreRoute(“{resource}.aspx/{*pathInfo}”);

以上四个步骤完成后,我们便可以开始添加ASP.NET
MVC控制器和视图了,例如:

using
System.Web.Mvc;
namespace MixingBothWorldsExample.Controllers
{
   public
class HomeController : Controller
   {
       public
ActionResult Index()
       {
           ViewData[“Message”]
= “This is ASP.NET MVC!”;
           return
View();
       }
   }
}

 

<%@
Page Language=”C#”
   AutoEventWireup=”true”
   CodeBehind=”Index.aspx.cs”
   Inherits=”MixingBothWorldsExample.Views.Home.Index”
%>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
 “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1″ runat=”server“>
   <title></title>
</head>
<body>
   <div>
       <h1><%=Html.Encode(ViewData[“Message”])
%></h1>
   </div>
</body>
</html>

admin

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注