Search code examples
razorasp.net-mvc-5html-helperembedded-resourceportable-class-library

Asp.net MVC 5 cannot use Html Helpers in a Razor view inside a class library project


Am making a Plugin architecture application using Asp.Net MVC 5 so I created a class library project that will contains some controllers and some embedded Razor views. everything works fine i made some references to the followingDlls `System.Web.Mvc/System.Web.Optimazations/System.Web.Razor/System.Web.WebPages/System.Web.WebPages.Razor/System.Web.helpers.

I added a web.config file and this is what it contains :

<?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=5.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" />
        <add namespace="Plugin_1" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

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

  <system.web>
    <compilation debug="true" targetFramework="4.5.2" >


    </compilation>

    <httpRuntime targetFramework="4.5.2" />
    <pages>
      <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" />
        <add namespace="Plugin_1" />
        <add namespace="Kendo.Mvc.UI"/>
      </namespaces>
    </pages>
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>

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

I can use the @model keyword etc but i cannot use html helper like "@Html.ActionLink() etc , @Html is not recognized to vs 2015 !! Can any one help me please i've seen some similar questions here on StackOF but it doesnt help. and Thank you.


Solution

  • To make Intellisense show the ASP.NET MVC specific properties you need to change the output path of your class library to bin:

    Right click project -> Properties -> Build -> change Output path to "bin/"

    Now if you open a Razor file in your class library you will get Intellisense for things like Html.ActionLink.