You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC5 Razor视图中@Raw语句不存在的问题求助

Troubleshooting the "@Raw does not exist in current context" Error with Postal.net in ASP.NET MVC5

Hi there! It sounds like you've got a solid foundation for reusing your email library and templates, but that @Raw error is a pesky roadblock. Let's walk through targeted fixes to get this sorted out:

1. Check the web.config in your email views folder

The most likely culprit is that your *~\Content\email* directory doesn’t have its own web.config file referencing the necessary MVC Html helper namespaces. Razor views prioritize configuration from their immediate directory’s web.config over the root app’s config, so this is easy to overlook.

Create or update the web.config in your ~/Content/email/ folder to explicitly include the System.Web.Mvc.Html namespace (where the Raw() method lives):

<?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.2.2.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" /> <!-- Critical for @Raw functionality -->
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="_MyWebAppName_" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

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

2. Test with a manual @using directive

As a quick sanity check, add @using System.Web.Mvc.Html at the top of your problematic email view file (the .cshtml throwing the error). If this fixes the issue, it confirms the namespace isn’t being loaded for that view, pointing directly back to the directory-level web.config needing adjustment.

3. Verify Postal.net's view engine configuration

Postal.net uses a custom view engine, so you might need to explicitly register additional namespaces for its Razor views. Add this code to your app’s startup (e.g., Global.asax.cs) to ensure the Html helpers are available:

var razorEngine = Postal.EmailViewEngine.Engines.OfType<RazorViewEngine>().FirstOrDefault();
if (razorEngine != null)
{
    razorEngine.Namespaces.Add("System.Web.Mvc.Html");
}

4. Check for assembly version conflicts

Make sure all your MVC and WebPages assemblies are aligned in version:

  • Confirm System.Web.Mvc v5.2.2.0 is referenced in your new web app
  • Verify System.Web.WebPages.Razor v3.0.0.0 matches the version specified in your config sections
  • Clean your project’s bin and obj folders, then rebuild to clear any cached assembly references

5. Clear Razor compilation cache

Stale compiled views can cause unexpected errors. Delete the contents of:

  • %LocalAppData%\Temp\Temporary ASP.NET Files for your app’s application pool
  • Your project’s bin and obj directories
    Then rebuild and run the app again.

内容的提问来源于stack exchange,提问作者mmr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:16:13