转换的Web应用程序到ASP.NET MVC 3 - 如何使剃刀看法? [英] Converting web app to ASP.NET MVC 3 - how to enable Razor views?

查看:183
本文介绍了转换的Web应用程序到ASP.NET MVC 3 - 如何使剃刀看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换使用的这个链接。

在添加视图我有一个选项添加剃刀或ASPX视图类型。当使用ASPX类型(<%:ViewBag.Message%> )的一切工作正常,但如果我尝试使用剃刀类型( @ViewBag。消息)我得到一个错误

When adding views I have an option to add razor or an aspx view types. When using aspx type ( <%: ViewBag.Message %> ) everything works fine but if I try to use razor type ( @ViewBag.Message ) I get an error

CS0103: The name 'ViewBag' does not exist in the current context

视觉工作室智能感知识别ViewBag类,但运行时没有。 (我使用Visual stuido开发服务器不在本地IIS)。

Visual Studios intellisense recognizes ViewBag class but runtime doesn't. (I'm using visual stuido dev server not local IIS).

我搜索周围的网站这条消息,一切都我发现建议更新的web.config(我做到了),或者运行Mvc3AppConverter,既不那些帮助过我。

I searched around the site for this message and everything I found suggested to update web.config (which I did) or run the Mvc3AppConverter, and neither of those helped me.

修改:我的web配置是这样的:

EDIT: My web config looks like this:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="ShowLanguageReload" value="true" />
    <add key="webpages:Version" value="1.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

  <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=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" />
      </assemblies>
    </compilation>
    <pages pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
      <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>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

此外,它包括,和的appSettings一些额外的按键,但是我删除了这些,因为我相信他们是不相关的我的问题。

Also it includes , and some additional keys in the appSettings, but I removed those as I believe they are not relevant to my question.

编辑#2 我也有在浏览一个web.config \共享文件夹,它看起来是这样的:

EDIT #2 I also have a web.config in Views\Shared folder it looks like this:

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.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=3.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.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>

    <pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
      <controls>
        <add assembly="System.Web.Mvc, Version=3.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>

任何其他建议?

Any other suggestions?

推荐答案

这两个可能会有所帮助:

Two Scott Hanselman links

These two may help:

  1. <一个href="http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx"相对=nofollow>集成ASP.NET MVC 3到现有的升级ASP.NET 4 Web窗体应用程序
  2. 混合剃刀视图和WebForms的母版页的ASP.NET MVC 3
  1. Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications
  2. Mixing Razor Views and WebForms Master Pages with ASP.NET MVC 3

不要忘记,改变根文件夹的的web.config 是远远不够的。您还需要有在配置剃刀的浏览文件夹相应的的web.config 文件。

Configuration

Don't forget that changing root folder's web.config isn't enough. You also need to have appropriate web.config file in the Views folder that configures Razor.

,因为它似乎你有太多的事情在你的浏览\ web.config中。我想借此准系统 web.config中的MVC VS项目模板,因为你配置的东西,不喜欢这里的appSettings需要配置提供文件等。

As it seems you have too many things in your Views\web.config. I would take the barebones web.config file provided by the MVC VS project template because you're configuring things that don't need configuration here like appSettings etc.

这篇关于转换的Web应用程序到ASP.NET MVC 3 - 如何使剃刀看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆