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.

6
  • 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. Commented Feb 17, 2015 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. Commented Feb 17, 2015 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 (look at my answer on that question). Commented Feb 17, 2015 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. Commented Feb 17, 2015 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 ;) Commented Feb 17, 2015 at 22:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.