【问题标题】:ServiceStack turn on Razor intellisense support without MVCServiceStack 在没有 MVC 的情况下开启 Razor 智能感知支持
【发布时间】:2012-09-25 03:00:59
【问题描述】:

我已将 SS.Razor 安装到我的测试项目中。如果我只是更改 default.htm -> cshtml,它可以工作,但没有 vs intellisense 语法支持。所以剃须刀代码是黑白的纯文本。

我想知道如何在不将项目作为 .net MVC 项目打开的情况下打开 Razor。谢谢!

编辑 ------------------------------------------

这是我的 web.config

(注意添加扩展名=“.cshtml”...有没有...)

<?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>

【问题讨论】:

    标签: c# razor servicestack


    【解决方案1】:

    为了在 VS.NET 中获得智能感知,您的 ASP.NET Web 应用程序需要注册必要的构建提供程序,这些提供程序告诉 VS.NET 编译剃刀视图、要包含的基类以及要添加的命名空间。您可以在Razor Rockstars example project 中找到必要的Web.Config 示例。

    注意:当您创建自托管控制台应用程序时,您还需要存储在 Web.config 中的 App.Config 的副本(这是 VS.NET 在设计时查看的内容)。 Web.config 在开发/设计时间之后不再需要或使用。

    安装ServiceStack.Razor NuGet 包时应该已经自动包含必要的 web.config 信息。因此,如果您删除了应用于 Web.config 的 web.config transform,则需要将其添加回来。如果您想手动复制 web.config.transform 文件,您需要使用您的项目命名空间手动修改 $rootnamespace$ 占位符,例如更改:

    <add namespace="$rootnamespace$" />
    

    <add namespace="Amce.MyWebProject" />
    

    嗯,看起来 VS 2012 上的智能感知可能存在一个突出问题,您需要恢复使用 VS2010,直到我们弄清楚 VS.NET 团队对 Razor 的构建提供程序所做的更改。

    【讨论】:

    • 我在关闭文本编辑器选项卡并再次打开 cshtml 文件后发现,它可以工作。但是即使您打开选项卡并关闭 VS 并重新启动它也不起作用。仅当您关闭该选项卡时才有效...对于 vs2010 和 vs2012 也是如此。
    【解决方案2】:

    除了确保您在 web.config 中定义了正确的 &lt;buildProviders&gt;,如果您在 VS 中打开 .cshtml 文件时看到以下警告。

    ASP.NET 运行时错误:无法加载文件或程序集“ServiceStack.Razor”或其依赖项之一。系统找不到指定的文件。

    您需要将 ServiceStack.*.dll 和 System.Web.Razor.dll 复制到 /bin 目录中。没错,不是 /bin/debug 或 /bin/release 目录。即使在自托管项目中,这也将为您提供完整的 Intellisense 支持(无需创建 Web 项目)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多