ASP.NET MVC5 Razor视图中@Raw语句不存在的问题求助
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.Mvcv5.2.2.0 is referenced in your new web app - Verify
System.Web.WebPages.Razorv3.0.0.0 matches the version specified in your config sections - Clean your project’s
binandobjfolders, 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 Filesfor your app’s application pool- Your project’s
binandobjdirectories
Then rebuild and run the app again.
内容的提问来源于stack exchange,提问作者mmr

