简体   繁体   English

ASP.net MVC4 WebAPI JSON中等信任

[英]ASP.net MVC4 WebAPI JSON Medium Trust

So, I've been playing with ASP.net MVC4 and the new WebAPI the past few days. 所以,过去几天我一直在玩ASP.net MVC4和新的WebAPI。 One of the first things I always do when starting a new app is throw <trust level="Medium" /> into my Web.config so I can be sure my app will run on most hosts. 我开始使用新应用时首先要做的事情之一就是将<trust level="Medium" />抛入我的Web.config中,这样我就可以确定我的应用程序可以在大多数主机上运行。

Usually, if I use the stock Microsoft stuff everything works without a problem. 通常情况下,如果我使用股票微软的东西一切正常,没有问题。 But tonight I was having problems following Brad Wilson's Webstack of Love presentation . 但今晚我遇到布拉德威尔逊的Webstack of Love演讲时遇到了问题。 All of my AJAX calls were returning a Server 500 error. 我的所有AJAX调用都返回了Server 500错误。 However, when I would hit the WebAPI directly in the browser, it would return everything in XML format. 但是,当我直接在浏览器中点击WebAPI时,它将以XML格式返回所有内容。

So I assume the serialization to JSON withing the WebAPI controller is what is causing the issue. 所以我假设使用WebAPI控制器对JSON进行序列化是导致问题的原因。 My question is how to work around this? 我的问题是如何解决这个问题?

It bothers me that Microsoft teaches shared hosts to play it safe and lock things down to Medium Trust, then creates new web technology that won't work in Medium Trust. 令我困扰的是,Microsoft教授共享主机以保证安全并将内容锁定到Medium Trust,然后创建新的Web技术,该技术在Medium Trust中不起作用。 Maybe everything will be fine when it is a final release and the binaries are installed in the GAC? 也许一切都会好,当它是最终版本并且二进制文件安装在GAC中时? It can take months for shared hosts to upgrade to the latest and greatest. 共享主机可能需要数月才能升级到最新版本。

It seems like the biggest problem is usually Reflection--90%+ of the stuff I've tried that won't run on Medium Trust is due to this (For instance, I'd love to use AutoMapper instead of hand-coding ViewModel => Entity mappings). 似乎最大的问题通常是反射 - 我尝试过的不会在Medium Trust上运行的东西的90%+是由于这个原因(例如,我喜欢使用AutoMapper而不是手动编码ViewModel =>实体映射)。 Is there a way to confine Reflection to the assemblies within an app so it can become part of the Medium Trust feature set and make this problem a thing of the past? 有没有办法将Reflection限制在应用程序中的程序集中,这样它就可以成为Medium Trust功能集的一部分,并使这个问题成为过去的事情?


I set up Failed Request Tracing on IIS when I found that the Application_Error event wasn't even firing in my Global.asax. 当我发现Application_Error事件甚至没有在我的Global.asax中触发时,我在IIS上设置了失败的请求跟踪。 This is the exception being thrown. 这是抛出的异常。 Not sure how to get any more details. 不知道如何获得更多细节。

Warning -MODULE_SET_RESPONSE_ERROR_STATUS 
ModuleName ManagedPipelineHandler 
Notification 128 
HttpStatus 500 
HttpReason Internal Server Error 
HttpSubStatus 0 
ErrorCode 0 
ConfigExceptionInfo
Notification EXECUTE_REQUEST_HANDLER
ErrorCode The operation completed successfully. (0x0)

Link to sample project displaying the issue . 链接到显示问题的示例项目 Also enclosed in the base "Test" directory are screenshots of my fiddler tests: XML-fiddle.png & JSON-fiddle.png. 同样包含在基础“Test”目录中的是我的fiddler测试的截图:XML-fiddle.png和JSON-fiddle.png。

Web API works just fine in medium trust. Web API在中等信任中运行良好。 Also, since RC version, it 's not deployed to GAC anymore. 此外,自RC版本以来,它不再部署到GAC。 You can just xcopy Web API dlls onto the server and reference them (or simply just publish your project from VS or git publish it if your host support it, like Azure). 你可以只将xix Web API dll复制到服务器上并引用它们(或者只是从VS发布你的项目,或者如果你的主机支持它就像git一样发布它,比如Azure)。

You mention web stack of love. 你提到了爱的网络堆栈。 Well, it is SignalR that doesn't work in medium trust mode. 好吧,SignalR在中等信任模式下不起作用。 See here - according to David Folwer, it won't anytime soon https://github.com/SignalR/SignalR/issues/364 看到这里 - 根据David Folwer的说法,它不会很快出现https://github.com/SignalR/SignalR/issues/364

Finally, if you are getting error 500s always add this to Global.asax 最后,如果您收到错误500,请始终将其添加到Global.asax

 GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

to receive verbose errors. 接收详细错误。

My problem had to do with the serializing of Entities with circular relationships (in this case Countries => States, State => Country). 我的问题与循环关系的实体序列化有关(在这种情况下,Countries => States,State => Country)。 The JSON serializer was unable to handle this which led to a 500 error when requesting JSON. JSON序列化程序无法处理此问题,导致请求JSON时出现500错误。

I often use Entities directly for mocking things up and getting a prototype. 我经常直接使用实体来模拟事物并获得原型。 I always planned on using distinct View Models anyway, but I wanted to understand what was going on and why it was failing. 无论如何,我总是计划使用不同的View模型,但我想了解发生了什么以及失败的原因。

Kiran Challa's answer in my ASP.net forums question helped me get the error that was occuring: "The RelationshipManager object could not be serialized. This type of object cannot be serialized when the RelationshipManager belongs to an entity object that does not implement IEntityWithRelationships." Kiran Challa在我的ASP.net论坛问题中的答案帮助我得到了错误: “RelationshipManager对象无法序列化。当RelationshipManager属于不实现IEntityWithRelationships的实体对象时,无法序列化此类对象。”

That then led me to the Serializing Circular References with JSON.Net and Entity Framework on johnnycode.com. 然后,我引导我在johnnycode.com上使用JSON.Net和Entity Framework进行序列化循环引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM