2

Background: I am upgrading a Webforms project to an MVC. I want to do it slowly so I have added every thing I need to the project for MVC. One issue that I am having is with the Web.config in the Views folder. I have added the follow to the Views/Web.config file:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.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.30319.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.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

Issue: When I type things in the view such as @Viewbag and @Styles I get the red line under them that's simply saying that they don't exist. Others like @Html and @RenderBody() work just fine.

Assumption: I believe that the Views/Web.config is not working correctly. I think that none of the namespaces are being used in the views. Could it be that I need to add a reference to it somewhere.

KernelSanders213
  • 147
  • 1
  • 1
  • 10
  • 3
    The easiest thing to do when upgrading (in my opinion) is to create a new project using the Visual Studio templates, then compare it to your upgraded project and see what's different. – mason Feb 17 '15 at 19:04
  • I took your idea and I created 2 projects: one Webforms and one MVC. I went through the steps to turn the webforms into an MVC. I added and changed everything, but it still is not loading the namespaces in. I really don't want to add using statements to my .cshtml files. – KernelSanders213 Feb 17 '15 at 21:30
  • What happens if you build, without adding using statements? Does it compile? You may need to make sure it [compiles Razor pages](http://stackoverflow.com/questions/28289107/vs2013-does-not-compile-asp-net-mvc5-views) (look at my answer on that question). – mason Feb 17 '15 at 21:31
  • Thanks mason!!!! Your solution worked. I have been fighting with this all day! I just had to add the snippets of code to my .csproj file and then the namespacing started to work. – KernelSanders213 Feb 17 '15 at 21:49
  • Did you try a build *before* trying my solution? I'm thinking it would have built, just your Intellisense wasn't working. By the way, you can upvote my answer on that question ;) – mason Feb 17 '15 at 22:01
  • Yes, it did build just fine before, so your solution just fixed the intellisense. – KernelSanders213 Feb 18 '15 at 13:45

1 Answers1

1

It compiles fine, the problem is that your Intellisense isn't working. You can get it to update by forcing the Razor pages to compile with each build, as described in my answer here.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121