Reputation: 500
I have a web api 2.0 application I want to add view handling to.
I am using postal.mvc5 and want to send an email from within the application.
I have the regular controller routing solved, and am now down to the final error to solve.
The _ViewStart.cshtml page that is implemented with Postal.Mvc5 is:
@{
Layout = null;
}
My web.config page in the views folder:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Optimization"/>
<add namespace="Postal" />
</namespaces>
</pages>
</system.web.webPages.razor>
</configuration>
The error I am getting is:
Type 'ASP._Page_Views_Emails__ViewStart_cshtml' does not inherit from 'System.Web.WebPages.StartPage'.
The complete error is:
Source: System.Web ----Target Site: CheckAssignableType Timestamp: 10/23/2017 3:05:48 PM Message: Type 'ASP._Page_Views_Emails__ViewStart_cshtml' does not inherit from 'System.Web.WebPages.StartPage'. ----StackTrace: at System.Web.UI.Util.CheckAssignableType(Type baseType, Type type) at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp) at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(String virtualPath, Type requiredBaseType) at System.Web.WebPages.BuildManagerWrapper.CreateInstanceOfType[T](String virtualPath) at System.Web.WebPages.VirtualPathFactoryExtensions.CreateInstance[T](IVirtualPathFactory factory, String virtualPath) at System.Web.WebPages.VirtualPathFactoryManager.CreateInstanceOfType[T](String virtualPath) at System.Web.WebPages.VirtualPathFactoryExtensions.CreateInstance[T](IVirtualPathFactory factory, String virtualPath) at System.Web.WebPages.StartPage.GetStartPage(WebPageRenderingBase page, IVirtualPathFactory virtualPathFactory, String appDomainAppVirtualPath, String fileName, IEnumerable
1 supportedExtensions) at System.Web.WebPages.StartPage.GetStartPage(WebPageRenderingBase page, String fileName, IEnumerable
1 supportedExtensions) at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at Postal.EmailViewRenderer.RenderView(IView view, ViewDataDictionary viewData, ControllerContext controllerContext, ImageEmbedder imageEmbedder) at Postal.EmailViewRenderer.Render(Email email, String viewName) at Postal.EmailService.CreateMailMessage(Email email) at Postal.EmailService.Send(Email email) at Postal.Email.Send()
I know this is pretty unconventional, but how do I get around this last error?
Update on further debugging:
I use Enterprise Logging on the web service, and noticed that it wasn't sending error emails until the next call so my error emails were not being sent until the next call to the web service. I added a system.net section to the main web.config file with the smtp setup, and that actually made it worse. No emails were being sent while the web service was running.
I tried instantiating a regular web application instance in the global.asax file for the web api, but this seems to have no real effect.
I have added some routing to the web.config file per the answer below, but that also has no effect. The wep api 2 application has the same references and other configuration items that a regular web application has, but still has the problem.
One solution would be to recreate the web api 2 as a regular website with web api 2 added. There are lots of online references on that, and the original application that the web api 2 application was spun off of was a regular website with api capability.
From reading the literature, this duality problem I am dealing with is probably cured with .Net Core, but I can't use it on our current system.
I guess the next question is why sendmail is disabled while the web api 2 is running.
What about web api 2 would disable sendmail during a web api call?
Upvotes: 0
Views: 2263
Reputation: 500
I finally got the problem solved. It was a problem with the web.config file in the Emails folder used by Postal.
Normally, the web.config files contain the correct information when you instantiate a web application, and so the web.config file for the views folder inherits the necessary types.
When starting with a web api 2 application, those types are not defined in the hierarchy and therefore not automatically inherited by the view folder.
The web.config for the Views/Emails folder was modified to this:
<?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=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.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Optimization"/>
<add namespace="Postal" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false"/>
</appSettings>
</configuration>
The system then routed to the view and processed the email as expected.
The problems with emails going through as referenced above were caused by the email sending interface bombing in the middle of a send. That was not really a problem caused by web api 2.
Upvotes: 2