简体   繁体   English

在同一项目中使用Web API 2和Forms应用程序进行错误处理-如何设置以同时处理两者?

[英]Error handling with Web API 2 and Forms application in same project - how to setup to handle both?

I have a project that contains both a Web API 2 service and aspx forms . 我有一个同时包含一个Web API 2个服务和ASPX形式的项目。 But I am trying to set up error handling so I can send users to a 404 error page if they try putting in fake page in the url. 但是我正在尝试设置错误处理,因此,如果用户尝试在URL中放入假页面,我可以将其发送到404错误页面。 Of course the Web API also needs to send 400 status message back to the users who call the web service so I am trying to keep the functionality of both in place. 当然,Web API还需要将400状态消息发送回调用Web服务的用户,因此我试图将两者的功能保持在适当的位置。 The main problem is I am not sure how to best set up the web.config as it not working. 主要问题是我不确定如何最好地设置web.config,因为它无法正常工作。 In my project I have both an error.aspx and error404.aspx pages. 在我的项目中,我同时具有error.aspx和error404.aspx页面。

With this in the web.config file: 在web.config文件中:

<customErrors mode="RemoteOnly" defaultRedirect="~/error.aspx" redirectMode="ResponseRewrite">
  <error statusCode="404" redirect="~/error404.aspx"/>
</customErrors>

<!-- setting existingResponse="PassThrough" needed, otherwise web api 400 error messages will not return -->
    <httpErrors existingResponse="PassThrough" errorMode="Custom">
  <remove statusCode="404" subStatusCode="-1"/>
  <error statusCode="404" prefixLanguageFilePath="" path="/myApp/error404.aspx" responseMode="ExecuteURL"/>
</httpErrors>

My app will return something like the following when I put in a fake page: http://myServer/myApp/fake.aspx 当我放入假页面时,我的应用程序将返回类似以下的内容: http://myServer/myApp/fake.aspx

Server Error in '/myApp' Application. “ / myApp”应用程序中的服务器错误。 Runtime Error Description: An exception occurred while processing your request. 运行时错误说明:处理您的请求时发生异常。 Additionally, another exception occurred while executing the custom error page for the first exception. 此外,在执行第一个异常的自定义错误页面时发生另一个异常。 The request has been terminated. 该请求已终止。

However, I can get the 404 page to show up if I change in the web.config: <httpErrors existingResponse="Replace" But making this changes causes the web API 400 status code messages not to show up when I send something like the following from my controller's get function: Return BadRequest("Your parameter is invalid") but these status messages are very important for the consumer of the service. 但是,如果我在web.config中进行更改,则可以显示404页面: <httpErrors existingResponse="Replace"但是,进行此更改会导致在我发送类似以下内容时Web API 400状态代码消息显示从控制器的get函数获取:Return BadRequest(“ Your parameter is invalid”),但是这些状态消息对于服务的使用者非常重要。

Any ideas how I can set up the project so I can get both errors to show up? 有什么想法可以设置项目,以便同时显示两个错误吗? Setting <httpErrors existingResponse="Auto" allows the Web API errors to be sent back, but not the error404.aspx for bad forms requests. Setting <httpErrors existingResponse="Auto"允许将Web API错误发送回,但对于错误的表单请求则不允许发送error404.aspx。

One thing I was wondering, if the Global.asax file might help as shown here or is such a process as this even possible? 有一两件事我想知道,如果Global.asax文件可能有助于如图所示这里或者是这样一个过程,因为这甚至可能吗?

In fact no error pages should be setup in the web.config file at instead. 实际上,不应在web.config文件中设置任何错误页面。

I followed the technique outlined in this post: Custom error pages for non-existant directory/file Web API (not controllers) 我遵循了本文中概述的技术: 不存在的目录/文件Web API(非控制器)的自定义错误页面

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

相关问题 Web API - Web窗体项目安全性错误 - Web API - Web Forms Project Security Error 有关如何通过同一代码库维护Web应用程序项目和网站的建议 - Suggestions on how to maintain both a web application project and a website from the same codebase 在同一域中路由API项目和Web应用程序项目(Azure) - Routing API project and Web Application project in the same domain (Azure) Web forms 应用程序处理不同的主机名 - Web forms application handling different hostnames 如何在Windows应用程序和Web窗体应用程序中使用dll,即使它引用Web内容(如nUnit)? - How to use a dll in both windows application and web forms application even if it references web stuff (like nUnit)? 在Web Forms 3.5项目中引导Web Api - bootstrapping Web Api in Web Forms 3.5 project 在Web Forms项目下托管Web API - Host a Web API under a Web Forms project 如果错误处理ASP.net Web表单 - If error handling ASP.net web forms 如何将.NET Web应用程序项目(API)与Apache Cordova Project集成? - How to integrate .NET web application project (API) with Apache Cordova Project? 如何在同一解决方案中从 MVC 项目调试 Web API 项目 - How To Debug Web API Project From MVC Project In Same Solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM