by Jim
Sep 23, 2007 7:57 AM
.NET 2.0 has some new language features, among them the null coalescing operator (jQuery15200027220021543247763_1327460644870), which I stumbled on just a few days ago. I need to keep up with my reading.
This is going to be particularly nice when storing properties in the ViewState in ASP.NET.
protected string WidgetName
{
get { return ViewState["WidgetName"] != null ? (string)ViewState["WidgetName"] : ""; }
set { ViewState["WidgetName"] = value; }
}
becomes:
protected string WidgetName
{
get { return (string)ViewState["WidgetName"] ?? ""; }
set { ViewState["WidgetName"] = value; }
}
For base types like int, things are a little tricky because the comparison must be done with nullable types. We can do this:
protected int WidgetId
{
get { return (int)(ViewState["WidgetId"] ?? (object)0); }
set { ViewState["WidgetId"] = value; }
}
Or we can cast our object to a nullable type; the syntax is a little nicer this way. int? is implicitly cast to the return type of int
protected int WidgetId
{
get { return (int?)ViewState["WidgetId"] ?? 0; }
set { ViewState["WidgetId"] = value; }
}
by Jim
Sep 14, 2007 5:34 PM
We have a site that uses forms authentication, with the exception of a few pages. In one of these pages, we were getting a bunch of syntax errors and weird behavior, but only when the page was accessed anonymously.
Firebug was showing syntax errors in the ScriptResourse.axd file. This is supposed to programmatically render script for the UpdatePanels and such, but was rendering the login.aspx page instead; authentication had failed for the anonymous user, and the request was redirected to the login page. Of course, this is html and not valid javascript, thus the syntax error.
Because the script includes didn't render, and didn't define the classes needed for the UpdatePanels, the page also had errors on the following lines:
Sys.Application.initialize();
Sys.WebForms.PageRequestManager._initialize('ScriptManager',
document.getElementById('MasterPageForm'));
var mngr = Sys.WebForms.PageRequestManager.getInstance();
This solution to this issue is to allow anonymous access to the ScriptResource.axd "file" in web.config, like so:
<location path="ScriptResource.axd">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
And while you're at it there's a WebResource.axd as well:
<location path="WebResource.axd">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>