简体   繁体   English

如何处理WebApi错误404

[英]How to handle WebApi error 404

I'm implementing MVC 5 application with WebApi2 in a same domain. 我正在同一个域中使用WebApi2实现MVC 5应用程序。 My problem now is how to handle error 404 in WebApi. 我现在的问题是如何处理WebApi中的错误404。 I'm using routing in my WebApi. 我在WebApi中使用路由。

[RoutePrefix("myapi")]
public class MyApiController : ApiController 
{
[Route("value")]
public string myvalue()
{
return "value";
}
}

Now I have my url "/myapi/value" that returns value string. 现在我有我的URL "/myapi/value"返回值字符串。 The problem is I requested "/myapi/value/bla/bla" or any URL that is not in my API, it returns error 404. 问题是我请求"/myapi/value/bla/bla"或任何不在我的API中的URL,它返回错误404。

I tried this link http://weblogs.asp.net/imranbaloch/handling-http-404-error-in-asp-net-web-api , it only applies in a WebApi project but not in a MVC + WebApi Project. 我试过这个链接http://weblogs.asp.net/imranbaloch/handling-http-404-error-in-asp-net-web-api ,它只适用于WebApi项目,但不适用于MVC + WebApi项目。

Please help. 请帮忙。 TIA TIA

UPDATE: 更新:

Please read carefully!!!! 请仔细阅读!!!!

This will be the default page if you don't handle 404 in your WebApi. 如果您未在WebApi中处理404,这将是默认页面。 It includes here your physical path. 它包括你的物理路径。

在此输入图像描述

Updated: 更新:

I think the following has a better description on how to implement error handling for a WebApi 2+ project: http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling . 我认为以下对如何实现WebApi 2+项目的错误处理有更好的描述: http//www.asp.net/web-api/overview/error-handling/web-api-global-error-handling

There are two problems I had in my projects when combining the MVC and WebApi in the same project. 在同一个项目中组合MVC和WebApi时,我的项目有两个问题。

The first being that my error handling of my normal MVC pages did not work anymore. 第一个是我的普通MVC页面的错误处理不再起作用。 To resolve this I initialized the WebApi routing first, over the MVC controller routing and made some small adjustments in the web.config like described in your post. 为了解决这个问题,我首先通过MVC控制器路由初始化了WebApi路由,并在web.config中进行了一些小调整,如帖子中所述。

Secondly if you want to handle invalid webapi routings, for example the correct directory but with wrong values which can not be resolved. 其次,如果您想处理无效的webapi路由,例如正确的目录但具有无法解析的错误值。 try the solution in the "Global error handling" section of the below link: http://weblogs.asp.net/jongalloway/looking-at-asp-net-mvc-5-1-and-web-api-2-1-part-4-web-api-help-pages-bson-and-global-error-handling 尝试以下链接的“全局错误处理”部分中的解决方案: http//weblogs.asp.net/jongalloway/looking-at-asp-net-mvc-5-1-and-web-api-2- 1 -部分- 4 -网上API-求助页面,BSON-及全局错误处理

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

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