2017年1月25日 星期三

MVC Inputting a default image in case the src attribute of an html

主要網站圖片有些會使用外部連結,如圖(1)為一個外部連結的圖檔,但會發生此網站中斷或者失去連線,就無法在抓取此圖片,如圖(2)會看見無發在抓取此圖片,因此需要設定預設的圖片,如圖(3)當外部網站失校將會使用預設圖片


(1)                        (2)                             (3)

設定如下:
1. Add JavaScript code: 
     onerror="this.onerror=null; this.src='/Images/Default.png'" 

2. Setting web.config
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404"/>
      <error statusCode="404" path="404.html" responseMode="File"/>

    </httpErrors>


Ref: 
http://stackoverflow.com/questions/980855/inputting-a-default-image-in-case-the-src-attribute-of-an-html-img-is-not-vali
http://stackoverflow.com/questions/40724854/error-page-in-asp-mvc
http://blog.darkthread.net/post-2015-11-10-customerrors-and-httperrors.aspx