ServiceStack turn on Razor intellisense support wi

2019-04-28 15:26发布

问题:

I have installed SS.Razor into my test project. If i simply change default.htm -> cshtml, it works, but without vs intellisense syntax support. So the razor code is plain text black and white.

I wonder how to turn on Razor without opening up the project as a .net MVC project. Thank you!

EDIT ------------------------------------------

Here is my web.config

(note the add extension=".cshtml" ... is there ...)

<?xml version="1.0" encoding="utf-8"?>
<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>
  <appSettings>
    <add key="webPages:Enabled" value="false" />
  </appSettings>
  <connectionStrings />
  <!--
    For a description of web.config changes for .NET 4.5 see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.5" />
      </system.Web>
  -->
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
      <buildProviders>
        <add extension=".cshtml" type="ServiceStack.Razor.CSharpRazorBuildProvider, ServiceStack.Razor" />
      </buildProviders>
    </compilation>
    <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
    <authentication mode="Windows" />
    <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    <httpHandlers>
      <add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
    </httpHandlers>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
  </system.web>
  <!-- 
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
    </handlers>
  </system.webServer>
  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="ServiceStack.Razor.ViewPage">
      <namespaces>
        <add namespace="ServiceStack.Html" />
        <add namespace="ServiceStack.Razor" />
        <add namespace="ServiceStack.Text" />
        <add namespace="ServiceStack.OrmLite" />
        <add namespace="Backbone.Todos" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
</configuration>

回答1:

In order to get intelli-sense in VS.NET your ASP.NET web application needs to have the necessary build providers registered which tell VS.NET to compiler razor views, what base class to include and what namespaces you want added. You can find an example of the necessary Web.Config in the Razor Rockstars example project.

Note: When you're creating a self-hosting Console Application you also need a copy of the App.Config stored in Web.config (which is what VS.NET looks at, at design-time). The Web.config isn't needed or used beyond development/design time so does.

The necessary web.config info should already be automatically included when you install the ServiceStack.Razor NuGet package. So if you've deleted the web.config transform that was applied to your Web.config, you need to add it back. If you want to copy the web.config.transform file manually you need to manually modify the $rootnamespace$ placeholder with your project namespace, e.g change:

<add namespace="$rootnamespace$" />

to

<add namespace="Amce.MyWebProject" />

Hmmm, It looks like there may be an outstanding issue with intelli-sense on VS 2012, you will need to revert back to using VS2010 until we figure out what the VS.NET team has changed with Razor's build providers.



回答2:

On top of ensuring that you have the correct <buildProviders> defined in web.config, if you're seeing the following warning when opening .cshtml files in VS.

ASP.NET runtime error: Could not load file or assembly 'ServiceStack.Razor' or one of its dependencies. The system cannot find the file specified.

You need to copy ServiceStack.*.dll and System.Web.Razor.dll into the /bin directory. Thats right, NOT the /bin/debug or /bin/release directories. This will give you full Intellisense support even in a self-hosted project (without needing to create a web project).