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

  • 相关阅读:
    ES数据导入导出
    python Elasticsearch5.x使用
    http://elasticsearch-py.readthedocs.io/en/master/api.html
    Python Elasticsearch api
    es批量索引
    Razor字符串处理
    [.NET] ConfuserEx脱壳工具打包
    查看网页源码的时候找不到数据绑定
    HearthBuddy decompile
    Quickstart: Create and publish a package using Visual Studio (.NET Framework, Windows)
  • 原文地址:https://www.cnblogs.com/donhwa/p/css_file_not_found_exception.html
Copyright © 2011-2022 走看看