Tuesday, 9 November 2010

CS0234: The type or namespace name 'Html' does not exist in thenamespace 'System.Web.WebPages' (are you missing an assembly reference?)

So I have just updated from MVC 3 Preview 1 to MVC 3 RC. I missed out the beta as didn't have time to check it out. Here are the steps I followed to get everything to work correctly.

Updating Ninject


Previously I was using the IMvcServiceLocator interface with Ninject to provide dependency injection. MVC 3 no longer provides this interface, instead it uses a simpilar one called IDependencyResolver
public class NinjectDependencyResolver : IDependencyResolver
{
    private IKernel _kernel;

    private ILog _logger = LogManager.GetLogger(typeof(NinjectDependencyResolver));

    public NinjectDependencyResolver(IKernel kernel)
    {
        _kernel = kernel;
    }
    public object GetService(Type serviceType)
    {
        try
        {
            return _kernel.Get(serviceType);
        }
        catch (Exception exception)
        {
            // log the error
            _logger.Error("Error whilst trying to get ninject service", exception);
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return _kernel.GetAll(serviceType);
        }
        catch (Exception exception)
        {
            // log the error
            _logger.Error("Error whilst trying to get ninject service", exception);
            return new List<object>();
        }
    }
}




Update Razor Tempaltes so LayoutPage => Layout
remove @inherits System.Web.Mvc.WebViewPage from _layout.cshtml

change @RenderSection("SectionName", optional:true) => @RenderSection("SectionName", optional:false)


set web.config in views to

<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" requirepermission="false" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="pages" requirepermission="false" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    </section></section></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">
      </add></add></add></add></namespaces>
    </pages>
  </host></system>

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

    <!--<br/>        Enabling request validation in view pages would cause validation to occur<br/>        after the input has already been processed by the controller. By default<br/>        MVC performs request validation before a controller processes the input.<br/>        To change this behavior apply the ValidateInputAttribute to a<br/>        controller or action.<br/>    -->
    <pages br="">        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"&gt;
      <controls>
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagprefix="mvc">
      </add></controls>
    </pages>
  </system>

  <system .webserver="">
    <validation validateintegratedmodeconfiguration="false">

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

No comments:

Post a Comment