Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未找到WebApi的处理404_C#_Asp.net_Asp.net Web Api - Fatal编程技术网

C# 未找到WebApi的处理404

C# 未找到WebApi的处理404,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,我正在创建一个 Asp.NETMVC+AngularJS 应用程序。在WebApi控制器中,我处理过任何类型的异常(500404等)。另一方面,所有未处理的异常都会被捕获 Global.asax.cs 档案 应用程序错误 方法。 但是,如果应用程序中缺少一个完整的API,它会像预期的那样在浏览器控制台上显示404错误,但是在这种情况下,我也需要重定向到一个适当的UI(html/cshtml)。我如何处理这种类型的异常 p.S.我试过使用 自定义错误 房地产 Web.config 文件,但这是徒劳

我正在创建一个

Asp.NETMVC+AngularJS

应用程序。在WebApi控制器中,我处理过任何类型的异常(500404等)。另一方面,所有未处理的异常都会被捕获

Global.asax.cs

档案

应用程序错误

方法。 但是,如果应用程序中缺少一个完整的API,它会像预期的那样在浏览器控制台上显示404错误,但是在这种情况下,我也需要重定向到一个适当的UI(html/cshtml)。我如何处理这种类型的异常

p.S.我试过使用

自定义错误

房地产

Web.config


文件,但这是徒劳的。

您需要遵循以下步骤

  • 创建一个控制器来处理错误
  • 为该错误控制器配置路由
  • 使用自定义IHttpControllerSelector处理404错误
  • 如果在所选控制器中未找到匹配的操作方法,则将请求传递给上述自定义IHttpControllerSelector。 这可以使用自定义IHttpActionSelector处理
  • 最后,在global.asax.cs文件中注册自定义IHttpControllerSelector和IHttpActionSelector

  • 请遵循Imran Baloch编写的awesome教程了解详细信息

    以下是一些处理API错误以执行重定向的方法

    使用

    使用

    如果你想要一个角度的方式,你可以利用

    这是基于AngularJS 1.1.4的。如果您使用的是不同的版本,则可以应用类似的策略

    $(document)
        .ajaxError(function (event, jqXHR, ajaxSettings, thrownError) {
            if (jqXHR.status == 404) {
                window.location.replace("http://www.stackoverflow.com");
            }
        });
    
    $.ajaxSetup({
        global: true,
        error: function(xhr, status, err) {
            if (xhr.status == 404) {
               window.location.replace("http://www.stackoverflow.com");
            }
        }
    });
    
    $provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
      return {
        'response': function(response) {
          // do something on success
          return response || $q.when(response);
        },
    
       'responseError': function(rejection) {
          // do something on error
          if (canRecover(rejection)) {
            return responseOrNewPromise;
          }
          return $q.reject(rejection);
        }
      };
    });
    
    $httpProvider.interceptors.push('myHttpInterceptor');
    
    angular.module("globalErrors", ['appStateModule']).factory "myHttpInterceptor", ($q, $log, growl) ->
      response: (response) ->
        $log.debug "success with status #{response.status}"
        response || $q.when response
    
      responseError: (rejection) ->
        $log.debug "error with status #{rejection.status} and data: #{rejection.data['message']}"
        switch rejection.status
          when 404
            window.location.replace("http://www.stackoverflow.com");
          else
            console.log(rejection.status);
    
        # do something on error
        $q.reject rejection
    
    .config ($provide, $httpProvider) ->
      $httpProvider.interceptors.push('myHttpInterceptor')