Dave's Notebook

Infinite 302 Loop - How would you even know?

trav-053 It happens eventually to all web developers. It’s happened to me twice in the last week. You make one simple change to your web site and then you can no longer access it. When you browse to it in IE you get some completely useless “We can’t access the page you are looking for” error. In FireFox, it at least tells you that you have a “Redirect Loop” and in IE6, if you are fortunate enough to still be writing for that browser, you can see that it is blinking at you as it tries to retrieve the page(s) multiple times. But how do you know what’s redirecting to what? For that matter, other than running a browser that told you, how would you even know it was a 302 error?

Read More

ASP.NET Application_Error Detecting 404's

misc_vol3_046 For many of you, this is going to be a “Duh!” kind of post. But while working on this today, I found so many people asking this question and so many others giving the wrong answer, I’m compelled to post anyhow. If you know the answer, then you are welcome to stop reading now. I didn’t write this for you. I wrote this for the hundreds of people who will search for this information and won’t be able to find the answer. The fact of the matter is, that’s why I write most of what I write–so people searching for the information can find it. So here’s the question: I’ve set up an Application_Error event handler in my Global.asax file and I have implemented a server transfer for errors. Now I want to set up a specific page to handle 404 errors. How do I detect a 404 error and call the 404-specific page? The main answer to this question involves retrieving the exception that triggered the event in the first place. To do that, we call Server.GetLastError():

Read More

Tab Control ActiveTabIndex Lost on Postback

tran-land-01 I just got off the phone with a client who is using the MS-AJAX TabControl in one of his applications and any time he causes a postback, the tab resets to the first tab.

If you’ve never seen the problem, you’re lucky. There are a couple of ways around the problem. The first and easiest if it works in your situation is to put the tab in an update panel so that you never actually do a full postback.

Read More

ASP.NET MVC - Model != BLL or DAL

Last week I introduced the ASP.NET MVC framework by talking a bit about what the model, view and controller are. In the comments, John Meyer said,

I respectfully disagree with your claim that the model is your BLL. MVC is a UI layer pattern, and as such all models, views, and controllers are strictly in the UI level.

While historically, MVC has been described in the way I stated–while the ASP.NET MVC guys have also portrayed the Model as BLL or below–I have to agree with John. Here’s why: At least as far as ASP.NET is concerned, the model is inherited from a specific class. This means that any implementation code you place in the class will be forever tied to the class it inherits from.

Read More

ASP.NET Response.Redirect() and JavaScript

A toucan perched on a branch in Brazil. Yesterday we covered issues surrounding using ASP.NET’s Response.Redirect in server side code. We noted that not handing it correctly could prevent code from running on the server that we want to run. The other issue is emitting Javascript in the server side in association with Response.Redirect(). This also leads to unexpected problems if you aren’t thinking about what is actually happening with your code. Take this code as an example:

Read More

Unsafe Mode in C#

tp_vol4_006 One of the “advantages” of using CSharp instead of VB.NET is that if programmers want to, they have the option of bypassing the memory management of .NET and working with memory directly. This is called “unsafe” mode.

While I will show you how to use this keyword, I have to tell you up front that I’ve been using CSharp since Beta 2 of .NET 1.0 and I’ve NEVER needed to switch into unsafe mode to do any of the work that I’ve done.

Read More