【问题标题】:How to return 404 by default in Asp Net Web Api如何在 Asp Net Web Api 中默认返回 404
【发布时间】:2020-03-07 03:05:54
【问题描述】:

我有一个简单的Asp Net Web Api,有一个端点,它运行良好。但我想,当您在我的情况下调用根服务器(loclahost:8001/) 时,它会向我返回404,而不是HTTP Error 403.14 - Forbidden。我是ASP NET MVC 的新手,习惯了asp net core。请问,我怎样才能在这里返回404

也许路由配置会有所帮助:

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{id}",
                defaults: new { controller = "GPayController"}
            );
        }
    }

【问题讨论】:

    标签: c# asp.net asp.net-web-api error-handling


    【解决方案1】:

    在您的 web.config 文件中的 <system.webServer> 部分下添加此配置:

    <system.webServer>
      <handlers>
        <add name="StopDirectoryBrowsing" path="*." resourceType="Directory" verb="*" 
             preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
      </handlers>
    <system.webServer>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多