如何处理WebApi错误404

编程入门 行业动态 更新时间:2024-10-27 05:32:11
本文介绍了如何处理WebApi错误404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在同一域中用WebApi2实现MVC 5应用程序. 我现在的问题是如何处理WebApi中的错误404.我在WebApi中使用路由.

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

现在我有了返回值字符串的URL "/myapi/value".问题是我请求"/myapi/value/bla/bla"或我的API中没有的任何URL,它返回错误404.

我尝试了以下链接 weblogs.asp/imranbaloch/handling-http-404-error-in-asp-net-web-api ,它仅适用于WebApi项目,不适用于MVC + WebApi项目.

请帮助. TIA

更新:

请仔细阅读!!!!

如果您不处理WebApi中的404,则这将是默认页面. 它包括您的身体路径.

解决方案

已更新:

我认为以下内容对如何为WebApi 2+项目实施错误处理有更好的描述: www.asp/web-api/overview/error-handling/web-api-global-error-handling .

在同一个项目中结合使用MVC和WebApi时,我的项目存在两个问题.

首先是我对常规MVC页面的错误处理不再起作用.为了解决这个问题,我首先通过MVC控制器路由初始化了WebApi路由,并像您的帖子中所述对web.config进行了一些小的调整.

第二,如果您要处理无效的webapi路由,例如正确的目录,但具有无法解析的错误值.请尝试以下链接的全局错误处理"部分中的解决方案: weblogs.asp/jongalloway/looking-at -asp-net-mvc-5-1-and-web-api-2-1-part-4-web-api-help-pages-bson-and-global-error-handling

I'm implementing MVC 5 application with WebApi2 in a same domain. My problem now is how to handle error 404 in WebApi. I'm using routing in my 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. The problem is I requested "/myapi/value/bla/bla" or any URL that is not in my API, it returns error 404.

I tried this link weblogs.asp/imranbaloch/handling-http-404-error-in-asp-net-web-api, it only applies in a WebApi project but not in a MVC + WebApi Project.

Please help. TIA

UPDATE:

Please read carefully!!!!

This will be the default page if you don't handle 404 in your WebApi. 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: www.asp/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.

The first being that my error handling of my normal MVC pages did not work anymore. 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.

Secondly if you want to handle invalid webapi routings, for example the correct directory but with wrong values which can not be resolved. try the solution in the "Global error handling" section of the below link: weblogs.asp/jongalloway/looking-at-asp-net-mvc-5-1-and-web-api-2-1-part-4-web-api-help-pages-bson-and-global-error-handling

更多推荐

如何处理WebApi错误404

本文发布于:2023-11-13 21:38:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   错误   WebApi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!