zoukankan      html  css  js  c++  java
  • asp.net 页面 css中图片不存在引发的异常

    在web 开发中,css 中发生某个样式的图片不存在的情况机率还是挺大的。比如在一个用于渲染前端页面的样式表 foreground.css 中,有如下片段:

    body
    {
        background:url(Images/bg.gif);
    }

    用于设置页面 body 的背景。当图片 bg.gif 不存在,我们可能无法察觉,特别是样式表越来越丰富时。

    在 asp.net 页面中,我们时常会使用 ResolveUrl 还处理 样式文件 或 js文件的路径,来确保它们的路径正确。比如,我们在 default.aspx 页面中,使用了

    <link href="<%=ResolveUrl("~/Styles/foreground.css") %>" type="text/css" rel="Stylesheet" />

    当所链接的样式表中含了不存在的图片时,就会在引发“文件不存在”这样一个全局性应用错误,我们可以在 Global.asax中的Application_Error 捕获到。

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception objErr = Server.GetLastError().GetBaseException();
        string err = "Error in:" + Request.Url.ToString() +
                "Error Message: " + objErr.Message.ToString() +
                "Stack Trace:" + objErr.StackTrace.ToString();
        
        Server.ClearError();
    }
    
    

    我们可以在 Server.ClearError(); 这里设个断点,然后调试,接着我们会发现文件不存在引发了异常。

    error

    我想,我们或许应该在发布前确保CSS 中所用到的图片确实存在。不然,我们可能在这里捕获到成千上万个“文件不存在的异常”。

    代码
    System.Web.HttpException: 文件不存在。 在 System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response) 在 System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context) 在 System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state) 在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

    演示代码:/Files/donhwa/2010-05/css_file_not_found_exception.rar

  • 相关阅读:
    oracle后台进程详解
    解决表格撑开浏览器问题,即自动换行问题
    文件后缀名修改或添加——字符串转换
    Struts1 action重定向跳转 带参数
    js 正则表达式
    js获取焦点
    select值的获取及修改
    iframe自适应高度,根据src中页面来得到。
    今个忽然晓得,原来radio不是普通去获取值的!
    查询时,如何保存获取相关路径url
  • 原文地址:https://www.cnblogs.com/donhwa/p/css_file_not_found_exception.html
Copyright © 2011-2022 走看看