• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    ASP.NET mvc异常处理的方法示例介绍
    1.首先常见保存异常的类(就是将异常信息写入到文件中去)
    复制代码 代码如下:

    public class LogManager
    {
    private string logFilePath = string.Empty;
    public LogManager(string logFilePath)
    {
    this.logFilePath = logFilePath;
    FileInfo file = new FileInfo(logFilePath);
    if (!file.Exists)
    {
    file.Create().Close();
    }
    }
    public void SaveLog(string message, DateTime writerTime)
    {
    string log = writerTime.ToString() + ":" + message;
    StreamWriter sw = new StreamWriter(logFilePath, true);
    sw.WriteLine(log);
    sw.Close();
    }
    }

    2、控制器异常处理

    这种方式就在需要进行异常处理的controller中重写OnException()方法即可,因为它本身继承了IExceptionFilter接口
    复制代码 代码如下:

    public class ExceptionController : Controller
    {
    public ActionResult Index()
    {
    throw new Exception("我抛出异常了!");
    }
    protected override void OnException(ExceptionContext filterContext)
    {
    string filePath = Server.MapPath("~/Exception。txt");
    StreamWriter sw = System.IO.File.AppendText(filePath);
    sw.WriteLine(DateTime.Now.ToString() + ":" + filterContext.Exception.Message);
    sw.Close();
    base.OnException(filterContext);
    Redirect("/");
    }
    }

    3、过滤器异常处理
    复制代码 代码如下:

    namespace MyMVC.Controllers
    {
    public class ExceptionController : Controller
    {
    [Error]
    public ActionResult Index()
    {
    throw new Exception("过滤器异常!");
    }
    }
    }
    public class ErrorAttribute : HandleErrorAttribute
    {
    public override void OnException(ExceptionContext filterContext)
    {
    base.OnException(filterContext);
    string path = filterContext.HttpContext.Server.MapPath("~/Exception.txt");
    StreamWriter sw = System.IO.File.AppendText(path);
    sw.WriteLine(DateTime.Now.ToString()+":"+filterContext.Exception.Message);
    sw.Close();
    }
    }
    您可能感兴趣的文章:
    • asp.net core MVC 全局过滤器之ExceptionFilter过滤器(1)
    • Asp.net Mvc 身份验证、异常处理、权限验证(拦截器)实现代码
    • 详解使用Spring MVC统一异常处理实战
    • springboot springmvc抛出全局异常的解决方法
    • ASP.NET MVC异常处理模块详解
    • MVC异常处理详解
    • ASP.NET MVC下基于异常处理的完整解决方案总结
    • 基于SpringMVC的全局异常处理器介绍
    • ASP.NET MVC中异常处理&自定义错误页详析
    • ASP.NET MVC中异常Exception拦截的深入理解
    上一篇:ASP.NET中application对象的使用介绍
    下一篇:在Repeater控件中通过Eval的方式绑定Style样式代码
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    ASP.NET mvc异常处理的方法示例介绍 ASP.NET,mvc,异常,处理,的,