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