How can I catch a 404?

try { var request = WebRequest.Create(uri); using (var response = request.GetResponse()) { using (var responseStream = response.GetResponseStream()) { // Process the stream } } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) { var resp = (HttpWebResponse) ex.Response; if (resp.StatusCode == HttpStatusCode.NotFound) { // Do something } else { // … Read more

Returning a 404 from an explicitly typed ASP.NET Core API controller (not IActionResult)

This is addressed in ASP.NET Core 2.1 with ActionResult<T>: public ActionResult<Thing> Get(int id) { Thing thing = GetThingFromDB(); if (thing == null) return NotFound(); return thing; } Or even: public ActionResult<Thing> Get(int id) => GetThingFromDB() ?? NotFound(); I’ll update this answer with more detail once I’ve implemented it. Original Answer In ASP.NET Web API 5 … Read more

Image Get Requests with AngularJS

Try replacing your src with ng-src for more info see the documentation: Using Angular markup like {{hash}} in a src attribute doesn’t work right: The browser will fetch from the URL with the literal text {{hash}} until Angular replaces the expression inside {{hash}}. The ngSrc directive solves this problem. <div ng-controller=”Cont”> <img ng-src=”https://stackoverflow.com/questions/12922509/{{imageSource}}”> </div>

Google Analytics – Failed to load resource: http://www.google-analytics.com/ga.js

It was a problem with AdBlock. I disabled it and now it loads it normally. yagudaev suggests (read answers below) that in order to keep AdBlock from blocking Google Analytics, you need to edit the snippet provided and explicitly use https:// instead of the protocol-relative URL by default. This means changing ‘//www.google-analytics.com/analytics.js’ into ‘https://www.google-analytics.com/analytics.js’ Example: … Read more

Servlet returns “HTTP Status 404 The requested resource (/servlet) is not available”

Introduction This can have a lot of causes which are broken down in following sections: Put servlet class in a package Set servlet URL in url-pattern @WebServlet works only on Servlet 3.0 or newer javax.servlet.* doesn’t work anymore in Servlet 5.0 or newer Make sure compiled *.class file is present in built WAR Test the … Read more

Routing for custom ASP.NET MVC 404 Error page

I’ve tried to enable custom errors on production server for 3 hours, seems I found final solution how to do this in ASP.NET MVC without any routes. To enable custom errors in ASP.NET MVC application we need (IIS 7+): Configure custom pages in web config under system.web section: <customErrors mode=”RemoteOnly” defaultRedirect=”~/error”> <error statusCode=”404″ redirect=”~/error/Error404″ /> … Read more