.NET MVC StackOverflow exception after returning View

六眼飞鱼酱① 提交于 2019-12-24 17:29:48

问题


I'm getting a 'System.StackOverflowException' in my .NET MVC project after the View has been sent to the Client. The View renders correctly, so the Client side sees no error. However, the server crashes with the StackOverflow Exception afterwards. I've only tested this in IISExpress/VisualStudio, not in IIS.

I have html files on a remote server. I'm loading the html files like this:

using (WebClient client = new WebClient() 
{Credentials = new NetworkCredential(Username, Password)})
{
    string html = client.DownloadString(url);
    viewModel.Pages.Add(html);
}

Where 'Pages' can contain multiple html pieces. However, the test page I'm using is a full html page, with "doctype", "head", "body", etc. nodes.

In my View, I'm rendering the page like this:

foreach(var page in Model.Pages)
{
    @Html.Raw(page)
}

As I said, the page renders correctly, but the server crashes afterwards. I don't see any recursive calls being made. I tried to use Debug Diag tool to troubleshoot, but couldn't get any crashdumps to populate.

Also, if I render the 'page' without the @Html.Raw() the crash doesn't occur ( However, the page then doesn't render correctly. e.g. the LT/GT brackets are replaced by "& gt;" / "& lt;", among other characters )

I suspect that it may have something to do with the length of the 'html' string, or the content of the page. I have another sitemap page that is simpler, loads correctly, and doesn't cause the server error.

The exceptions are occurring in different places as well: System.Web.dll, mscorlib.dll, and System.Runtime.Serialization.dll

How can I troubleshoot this StackOverflow Exception?

Is there a limit on the size of an string that can be rendered with @Html.Raw()?

External Stack Trace:

System.Runtime.Serialization.dll!System.Runtime.Serialization.Json.XmlJsonWriter.HasOpenAttribute.get() Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.Json.XmlJsonWriter.WriteState.get() Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.Json.XmlJsonWriter.WriteStartAttribute(string prefix, string localName, string ns) Unknown System.Xml.dll!System.Xml.XmlWriter.WriteAttributeString(string localName, string value) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.Json.JsonWriterDelegator.WriteInt(int value) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.IntDataContract.WriteXmlValue(System.Runtime.Serialization.XmlWriterDelegator writer, object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.Json.JsonDataContract.WriteJsonValueCore(System.Runtime.Serialization.XmlWriterDelegator jsonWriter, object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.WriteDataContractValue(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle declaredTypeHandle) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool verifyKnownType, System.RuntimeTypeHandle declaredTypeHandle, System.Type declaredType) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.SerializeWithXsiType(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle objectTypeHandle, System.Type objectType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle, System.Type declaredType) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerialize(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) Unknown [Lightweight Function]
System.Runtime.Serialization.dll!System.Runtime.Serialization.Json.JsonCollectionDataContract.WriteJsonValueCore(System.Runtime.Serialization.XmlWriterDelegator jsonWriter, object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.WriteDataContractValue(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle declaredTypeHandle) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool verifyKnownType, System.RuntimeTypeHandle declaredTypeHandle, System.Type declaredType) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.SerializeWithXsiType(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle objectTypeHandle, System.Type objectType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle, System.Type declaredType) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) Unknown System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerialize(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) Unknown

The portion from [Lightweight Function] to the end repeats until the crash.

Edit:

Just found this error (among others) on the Output window: A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll


回答1:


Solved this by returning 'Content' instead of 'View(model)'.

Previously, I was adding the html string to a ViewModel and rendering the page by returning:

return View(model);

When I changed the return to Content, the server did not crash.

return Content(htmlString);


来源:https://stackoverflow.com/questions/31575665/net-mvc-stackoverflow-exception-after-returning-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!