在ASP.Net Web Api(global.asax)中全局处理所有异常

时间:2018-08-30 23:04:38

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

我目前正在var icons = $('a.dropdown-toggle > i'); var toggles = $('a.dropdown-toggle'); //function to show the corresponding dropdown when a toggle is clicked function ShowDropdown(toggle) { //get the dropdown from the ID (assigned via data-toggle in the HTML) var dropdown = $(toggle.data('toggle')); //hide all other dropdowns - nothing will happen if they're already hidden $('.dropdown').not(dropdown).slideUp(); //ensure dropdown is in correct location, and slide it down dropdown.css({ left: toggle.offset().left, top: toggle.offset().top + toggle.outerHeight(true) }).slideDown(); } //map the click event toggles.click(function() { //find the current icon, and change its classes var icon = $(this).find('i').toggleClass('fa-chevron-up fa-chevron-down'); //remove the down class and add the up class to all others icons.not(icon).removeClass('fa-chevron-down').addClass('fa-chevron-up'); //if the dropdown is now selected... if(icon.hasClass('fa-chevron-down')) { //...show the dropdown... ShowDropdown($(this)); } //...otherwise... else { //...hide the dropdown $($(this).data('toggle')).slideUp(); } }); 中使用public partial class Form1 : Form { public Form1() { InitializeComponent(); Bitmap image = DataConverter2d.ReadGray(StandardImage.LenaGray); Array2d<double> dImage = DataConverter2d.ToDouble(image); int newWidth = Tools.ToNextPowerOfTwo(dImage.Width) * 2; int newHeight = Tools.ToNextPowerOfTwo(dImage.Height) * 2; double n = 6; double f0 = 0.5; double theta = 60; double a = 0.4; Array2d<Complex> kernel2d = CustomFft(newWidth, newHeight, n, f0, theta, a); dImage.PadTo(newWidth, newHeight); Array2d<Complex> cImage = DataConverter2d.ToComplex(dImage); Array2d<Complex> fImage = FourierTransform.ForwardFft(cImage); // FFT convolution ................................................. Array2d<Complex> fOutput = new Array2d<Complex>(newWidth, newHeight); for (int x = 0; x < newWidth; x++) { for (int y = 0; y < newHeight; y++) { fOutput[x, y] = fImage[x, y] * kernel2d[x, y]; } } Array2d<Complex> cOutput = FourierTransform.InverseFft(fOutput); Array2d<double> dOutput = Rescale2d.Rescale(DataConverter2d.ToDouble(cOutput)); dOutput.CropBy((newWidth - image.Width) / 2, (newHeight - image.Height) / 2); Bitmap output = DataConverter2d.ToBitmap(dOutput, image.PixelFormat); Array2d<Complex> cKernel = FourierTransform.InverseFft(kernel2d); cKernel = FourierTransform.RemoveFFTShift(cKernel); Array2d<double> dKernel = DataConverter2d.ToDouble(cKernel); Array2d<double> dLimitedKernel = Rescale2d.Limit(dKernel); Bitmap kernel = DataConverter2d.ToBitmap(dLimitedKernel, image.PixelFormat); pictureBox1.Image = image; pictureBox2.Image = kernel; pictureBox3.Image = output; } private double Basic(double u, double v, double n, double f0, double rad, double a, double b) { double ua = u + f0 * Math.Cos(rad); double va = v + f0 * Math.Sin(rad); double ut = ua * Math.Cos(rad) + va * Math.Sin(rad); double vt = (-1) * ua * Math.Sin(rad) + va * Math.Cos(rad); double p = ut/a; double q = vt/b; double sqrt = Math.Sqrt(p*p + q*q); return 1.0 / (1.0+ 0.414 * Math.Pow(sqrt, 2*n)); } private double Custom(double u, double v, double n, double f0, double theta, double a) { double rad1 = (Math.PI / 180) * (90-theta); double rad2 = rad1 + Math.PI; double b = (a / 5.0) / (2*n); double ka = Basic(u, v, n, f0, rad1, a, b); double kb = Basic(u, v, n, f0, rad2, a, b); return Math.Max(ka, kb); } private Array2d<Complex> CustomFft(double sizeX, double sizeY, double n, double f0, double theta, double a) { double halfX = sizeX / 2; double halfY = sizeY / 2; Array2d<Complex> kernel = new Array2d<Complex>((int)sizeX, (int)sizeY); for (double y = 0; y < sizeY; y++) { double v = y / sizeY; for (double x = 0; x < sizeX; x++) { double u = x / sizeX; double kw = Custom(u, v, n, f0, theta, a); kernel[(int)x, (int)y] = new Complex(kw, 0); } } return kernel; } } 来处理Web API异常,将其传递给错误记录器并进行记录。

Application_Error

它捕获了一些异常,但不是全部-例如服务器中的404错误等。我如何自己捕获更多异常(我认为这将意味着覆盖ASP.Net的默认异常处理)。是否可以通过global.asax来完成全部操作,或者还有其他方法?我想避免所有控制器上的 void Application_Error(object sender, EventArgs e) { if (Server.GetLastError() != null) { exceptionController.Log(Server.GetLastError()); Server.ClearError(); } } 块。

Mods-有100万个线程,但也有来自许多不同时间段的100万个同样独特的答案。我很困惑,以至于我不得不在2018年再问一次。

1 个答案:

答案 0 :(得分:0)

如果您要处理框架中的404,可以添加一条路由来处理“其他所有内容”并返回自定义响应。

在路由表的末尾有一条通吃的路由,看起来有点像

        config.Routes.MapHttpRoute(
            name: "NotImplemented",
            routeTemplate: "{*data}",
            defaults: new { controller = "Error", action = "notimplemented", data = UrlParameter.Optional });

我们在其中生成自定义响应。您也许可以在路由表中执行类似的操作。