Skip to content

Codepolice

  • ⤫

404 redirects in code with ASP.NET

Posted by Judy Alvarez Posted on February 25, 2022March 1, 2022
0

I have had problems doing proper 404 redirects in ASP.NET ever since I started to care about proper 404 pages. I do not have any trouble if the page does not exist. But say for example that I require a query string on some page and for some reason, I do not get a query string. Then I guess it’s proper to give a 404 error.  Today I decided to give it another try and finally, I think I have found the solution thanks to two great tips on the amazing Stackoverflow.com.

The first one is that if you want to throw a 404 error you can just do.

throw new HttpException(404, “Article not found”);

As simple as that. In almost all other articles I read about this people are talking about doing Response. Status = 404 and stuff like that. The problem with that is that the page will continue to execute and you most likely get some kind of error if something vital is missing.

The next thing I learned where that you could do like this.





Notice the “redirect mode” property. It will cause asp.net to not change the URL but do a “Server.Transfer” like redirect to the error page but keep the URL that throws the error.

Categories: JavascriptTagged: ajax in asp net, applications of asp net, asp net authentication and authorization, asp net authentication types, asp net books, asp net code, asp net components, asp net core microservices, asp net ecommerce, asp net file upload, asp net hidden field, asp net interview questions and answers pdf, asp net introduction, asp net mvc 4.0, asp net mvc web api, asp net programmer, asp net que es, asp net web development, asp net web site hosting, asp net website, curso asp net, file upload in asp net, gridview in asp net, hidden field in asp net, hire asp net developer, host asp net, net core vs asp net core, types of authentication in asp net, web api in asp net, web controls in asp net

Post navigation

Previous Previous post: Remote Desktop Connection (RDP) becomes extremely slow after installing “Intel Smart Connect Technology” update
Next Next post: New Relic and runAllManagedModulesForAllRequests flag in .net

Related Posts

  • What’s the story? JavaScript’s 30!

    #​764 — December 5, 2025 Read on the Web JavaScript Weekly 🎉  JavaScript Turns 30 Years Old  🎉 Back in May 1995, a 33 year old Brendan Eich built the first prototype of JavaScript in just ten days, originally codenamed Mocha (and then LiveScript). On December 4, 1995, Netscape and Sun Microsystems officially announced ‘JavaScript’ in a

    Posted by Posted on December 5, 2025
    0
  • Comparing performance across Node versions and ARM vs x86

    #​603 — December 2, 2025 Read on the Web Tinybench 6.0: A Tiny, Simple Benchmarking Library — Uses whatever precise timing capabilities are available (e.g. process.hrtime or performance.now). You can then benchmark whatever functions you want, specify how long or how many times to benchmark for, and get a variety of stats in return –

    Posted by Posted on December 2, 2025
    0
  • Algorithms visualized and demonstrated in JavaScript

    #​763 — November 28, 2025 Read on the Web JavaScript Weekly Over 150 Algorithms and Data Structures Demonstrated in JS — Examples of many common algorithms (e.g. bit manipulation, Pascal’s triangle, Hamming distance) and data structures (e.g. linked lists, tries, graphs) with explanations. Available in eighteen other written languages too. Oleksii Trekhleb et al. TypeScript: From

    Posted by Posted on November 28, 2025
    0
  • Guess who’s back, back again? Shai-Hulud.

    #​602 — November 25, 2025 Read on the Web How a Summer in Abruzzo Helped Bring Type Stripping to Node.js — Node.js TSC member and committer Marco tells the personal tale of what it took to bring type stripping (now considered stable) to Node. It’s neat to get the back story. He’s now working on

    Posted by Posted on November 25, 2025
    0
  • A significant Angular release

    #​762 — November 21, 2025 Read on the Web JavaScript Weekly Google Announces Angular v21 — The Google team has gone all out with this significant release of its popular JavaScript framework. They’ve put together a retro game-themed adventure-based tour of what’s new, along with top notch videos showing off features like its new signal-based

    Posted by Posted on November 21, 2025
    0
  • Did you know Node has a ‘deprecate’ method?

    #​601 — November 18, 2025 Read on the Web Node.js v25.2.1 (Current) Released (and 25.2.0 with Type Stripping Marked ‘Stable’) — v25.2.0 was released hours after we hit send last week (often the way!) and marked type stripping as stable, meaning all major server-side runtimes now support TypeScript officially (at least in type-stripping form). v25.2.1,

    Posted by Posted on November 18, 2025
    0
Judy Alvarez

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Codepolice

  • Github
  • Atlassian
  • Flatlogic
  • Xero
  • Jetbrains
  • Figma
  • What’s the story? JavaScript’s 30!
  • Comparing performance across Node versions and ARM vs x86
  • Algorithms visualized and demonstrated in JavaScript
  • Guess who’s back, back again? Shai-Hulud.
  • A significant Angular release
https://flatlogic.com/generator
COPYRIGHT © 2025 - Codepolice