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

  • Microsoft shrunk the TypeScript

    #​640 — May 25, 2023 Read on the Web JavaScript Weekly DeviceScript: TypeScript for Tiny Thingamabobs — DeviceScript is a new Microsoft effort to take the TypeScript experience to low-resource microcontroller-based devices. It’s compiled to a custom VM bytecode which can run in such constrained environments. (A bit like Go’s TinyGo.) It’s aimed at VS

    Posted by Posted on May 25, 2023
    0
  • Node.js 20 is fast(er)

    #​487 — May 23, 2023 Read on the Web The State of Node.js Performance in 2023 — Nearform’s Rafael Gonzaga reflects on the recent release of Node 20 and puts it through its paces against v18.16 and v16.20 with a few different benchmark suites running on a pretty typical EC2 instance. It goes into a

    Posted by Posted on May 23, 2023
    0
  • jQuery lives on; major changes teased

    #​639 — May 18, 2023 Read on the Web JavaScript Weekly Bun’s New Bundler: 220x Faster than webpack? — Bun is one of the newest JavaScript runtimes (built atop the JavaScriptCore engine) and focuses on speed while aiming to be a drop-in replacement for Node.js. This week’s v0.6.0 release is the ‘biggest release yet’ with

    Posted by Posted on May 18, 2023
    0
  • MS’s new tool for digging deep into V8

    #​486 — May 16, 2023 Read on the Web ‘This Page is Hosted on my Android Phone’ — Since it’s been linked on some pretty big sites already, I don’t feel too guilty about linking it here too 😆 This is a link to a blog being hosted from a $180 Android phone that’s running

    Posted by Posted on May 16, 2023
    0
  • Why Svelte is converting TypeScript to JSDoc

    #​638 — May 11, 2023 Read on the Web JavaScript Weekly The JavaScript Ecosystem is Delightfully Weird — There are plenty of examples of how JavaScript is weird but Sam focuses on the why. If you’ve been a JS developer for many years you’ll have seen it go through many phases and morph to fit

    Posted by Posted on May 11, 2023
    0
  • Getting recursive with Node 20.1

    #​485 — May 9, 2023 Read on the Web Node v20.1.0 (Current) Released — If you’re still catching your breath after the epic Node 20 release, don’t fear – 20.1 is a more typical bugfix, dependency update, and tweak-style affair. Michaël Zasso 💡 That said, the fs readdir and opendir functions get a new recursive option

    Posted by Posted on May 9, 2023
    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
  • Microsoft shrunk the TypeScript
  • Node.js 20 is fast(er)
  • jQuery lives on; major changes teased
  • MS’s new tool for digging deep into V8
  • Why Svelte is converting TypeScript to JSDoc
https://flatlogic.com/generator
COPYRIGHT © 2023 - Codepolice