473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Throwing a 404 when rewriting urls

Hi,

I'd just like some advice on the best way to throw a 404 when
rewriting urls. The scenario is as follows:

My url is /products/redwidgets

This redirects fine to ~products.aspx?id=redwidgets

I then do a db lookup to see if redwidgets exists, but it doesn't. At
this point I want to throw a 404 error.

Should I create a 404.aspx page and server.transfer to that, or is
there some other way?

Thanks.
Aug 8 '08 #1
3 1413
"IanW" <sp*******@tiscali.co.ukwrote in message
news:c9**********************************@k30g2000 hse.googlegroups.com...
Should I create a 404.aspx page and server.transfer to that, or is
there some other way?
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRe quest();
return;
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 8 '08 #2
So Do I put this code in the products.aspx page?

HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRe quest();
return;

I have the following in web.config, but the current solution does not
redirect to the actual error page. How would I accomplish that?

<customErrors mode="Off">
<error statusCode="404" redirect="~/404.aspx"/>
</customErrors>

On 8 Aug, 10:39, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
"IanW" <spondi...@tiscali.co.ukwrote in message

news:c9**********************************@k30g2000 hse.googlegroups.com...
Should I create a 404.aspx page and server.transfer to that, or is
there some other way?

HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRe quest();
return;

--
Mark Rae
ASP.NET MVPhttp://www.markrae.net
Aug 8 '08 #3
"IanW" <sp*******@tiscali.co.ukwrote in message
news:21**********************************@b1g2000h sg.googlegroups.com...

[top-posting corrected]
>>Should I create a 404.aspx page and server.transfer to that, or is
there some other way?

HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteR equest();
return;

So do I put this code in the products.aspx page?
Add it to your URL rewriting code, which I'm assuming is in
Application_BeginRequest...
I have the following in web.config, but the current solution does not
redirect to the actual error page. How would I accomplish that?

<customErrors mode="Off">
<error statusCode="404" redirect="~/404.aspx"/>
</customErrors>
Firstly, you've actually switched custom errors off, so this will never
work. See here for more details:
http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx

Secondly, the redirection will only happen for filetypes which ASP.NET knows
about e.g. .aspx etc. Other filetypes e.g. .htm, .html will never be caught
by this unless they have been specifically added to the list of filetypes
which IIS sends to ASP.NET for processing...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 8 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ricky Romaya | last post by:
Can I have somekind of URL rewriting mechanism without touching the web server's configuration (implemented in pure PHP) and win32 compatible? All the info I have teaches about apache's mod_rewrite...
6
by: Gordan | last post by:
Hi, here's my problem. On my pages I have URL souch as these "page.php?id=2&subpage=3&param=65". For security reasons I would like to add another parameter. It would be created like this...
7
by: go | last post by:
Can someone please tell me what URL rewriting is? I don't understand . . . a client is asking me to rewrite his URLs.
0
by: Lee | last post by:
Hi all ;) Preamble -------- I'm using URL rewriting to enforce a frames policy (yeah, I know frames are 'bad' :) - i.e. if a request comes in for a page which should be nested within a...
1
by: dedipya | last post by:
Environment W2003K with Oracle 10 client connected to oracle using oracle odp provider.orcle is on solaries machine. while filling the dataset .net is throughing the error "argument was out of the...
0
by: Moinak Bhattacharya | last post by:
Hi, We are using URL rewriting to display user friendly URL in the address bar for the pages requested. For this, physical pages are mapped to the logical urls by regexp in HTTPModules. But the...
3
by: =?Utf-8?B?bXVzb3NkZXY=?= | last post by:
Hi guys I have a web app I'm developing in C#. I have a UrlRewrite HTTP Handler, which seems to work, apart from when I ask it to do something if NOT. Here's the directory structure... ...
2
by: Seth Williams | last post by:
The first scenario is that I want to create one website for a particular industry - let's say for Farm Machinery so I have a shared hosting site called myFarmMachinery.com But I want to sell...
3
by: Toni | last post by:
Does anyone have a recommended method of rewriting URLs with classic ASP? For example, if the browser shows the Friendly URL www.mydomain.com/blue_widget my app will show the following Content...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.