CatchAllJavaScriptClientErrorsontheServerSide我找到了解决办法!我用过C#和MVC。添加一个新类来自定义您的js文件包,如下所示:}publicCustomScriptBundle(stringvirtualPath,stringcdnPath):base(virtualPath,cdnPath){Builder=newCustomScriptBundleBuilder();}}并且,创建另一个类来更改js文件的内容,如下所示:@file.IncludedVirtualPath));使用(varreader=fileInfo.OpenText()){returnreader.ReadToEnd();}}publicstringBuildBundleContent(Bundlebundle,BundleContextcontext,IEnumerablefiles){varcontent=newStringBuilder();foreach(varfileInfoinfiles){varcontents=newStringBuilder(Read(fileInfo));//获取catch块的常规expersionconststringpattern=@"bcatchb(s*)*((?([^)])*))(s*)*{(?([^{}])*({([^}])*})*([^}])*)}";varregex=newRegex(pattern);varmatches=regex.Matches(contents.ToString());for(vari=matches.Count-1;i>=0;i--)//从头到尾!(避免丢失索引){varmatch=matches[i];//catch(errVariable)varerrVariable=match.Groups["errVariable"].ToString();//catch块的起始索引varblockContentIndex=match.Groups["blockContent"].Index;varhasContent=match.Groups["blockContent"].Length>2;contents.Insert(blockContentIndex,string.Format("if(customErrorLogging)customErrorLogging({0}){1}",errVariable,hasContent?";":""));}varparser=newJSParser(contents.ToString());varbundleValue=parser.Parse(parser.Settings)。ToCode();content.Append(bundleValue);content.AppendLine(";");}returncontent.ToString();}}现在,将你的js文件包含在你的类的应用程序包中:BundleTable.Bundles.Add(newCustomScriptBundle("~/scripts/vendor").Include("~/scripts/any.js"));最后将customErrorLogging函数写在一个新的js文件中,如下所述,并将其添加到项目的主html表单中:“使用strict";varcustomErrorLogging=function(ex){//dosomething};window.onerror=function(message,file,line,col,error){customErrorLogging({消息:消息,文件:文件,行:行,列:col,error:error},this);returntrue;};现在你可以捕获应用程序中的所有异常并管理它们:)你可以使用try/catch块:try{myUnsafeFunction();//这可能会导致我们要处理的错误}catch(e){logMyErrors(e);//这里变量e包含有关错误的信息;用它做任何你想做的后处理}try"块并在"如果抛出错误(例如,以特定方式记录错误),则捕获”块。有更多选项可用:根据抛出的错误类型,您可以有多个“catch”块。更多信息,请访问:https://developer.mozilla.org/en-US/docs/Web/JavaScript/reference/statement/try...catch看一个如何捕获异常的小例子:以上是C#学习教程:catchserver如果对你有用,需要了解更多C#学习教程,希望大家多多关注——try{alert("properalert!");aert("错误这不是一个函数!");}catch(err){document.getElementById("demo").innerHTML=err.message;}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
