10. Advancements in ASP.NET 4.0
Web Forms
Set meta tags
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
Keywords="These, are, my, keywords"
Description="This is a description" %>
View State for Individual Controls
ViewStateMode (Enabled, Disabled, Inherit)
<asp:PlaceHolder ID="PlaceHolder1" runat="server"
<%@ Page Language="C#" AutoEventWireup="true"
ViewStateMode="Disabled">
CodeBehind="Default.aspx.cs"
<asp:PlaceHolder ID="PlaceHolder2" runat="server"
Inherits="WebApplication1._Default"
ViewStateMode="Enabled">
ViewStateMode="Disabled" %>
11. Advancements in ASP.NET 4.0
Web Forms
public class CustomProvider : HttpCapabilitiesEvaluator
Browser Capabilities Providers
{
Replacing ASP.NET Browser Capabilities
public override HttpBrowserCapabilities
1. GetBrowserCapabilities(HttpRequest request) and overrides
Create provider class from HttpCapabilitiesProvider
{
GetBrowserCapabilities method
HttpBrowserCapabilities browserCaps =
2. Register provider with application
base.GetHttpBrowserCapabilities(request);
– if (browserCaps.Browser == "Unknown")
browserCaps section in web.config or machine.config
{
– Write code in Application_Start (Global.asax)
browserCaps = MyBrowserCapabilitiesEvaulator(request);
}
Caching HttpBrowserCapabilities Object (same process)
return browserCaps;
Extending ASP.NET Browser Capabilities (same process)
}
}
12. Advancements in ASP.NET 4.0
Web Forms
Easier to useGlobal : System.Web.HttpApplication
public class
routing
{ protected void Page_Load(object sender, EventArgs e)
<asp:sqldatasource id="SqlDataSource1" runat="server"
{void Application_Start(object sender, EventArgs e)
PageRouteHandler class
connectionstring="<%$ ConnectionStrings:MyNorthwind %>"
{ string searchterm = Page.RouteData.Values["searchterm"] as
selectcommand="SELECT CompanyName,ShipperID FROM Shippers
New properties - HttpRequest.RequestContext, Page.RouteData
string;
RouteTable.Routes.MapPageRoute("SearchRoute",
where
label1.Text = builders -
expression searchterm;
New "search/{searchterm}", "~/search.aspx");
CompanyName=@companyname"
} RouteTable.Routes.MapPageRoute("UserRoute",
System.Web.Compilation.RouteUrlExpressionBuilder,
<selectparameters>
"users/{username}", "~/users.aspx");
<asp:routeparameter name="companyname" RouteKey="searchterm"
System.Web.Compilation.RouteValueExpressionBuilder
http://localhost/search/scott
}
/>
} RouteUrl
</selectparameters>
RouteValue
RouteTable.Routes.Add("SearchRoute", new
</asp:sqldatasource>
<asp:HyperLink ID="HyperLink1" runat="server"
Route("search/{searchterm}",
RouteParameter class
NavigateUrl="<%$RouteUrl:SearchTerm=scott%>">Search for
new PageRouteHandler("~/search.aspx")));
Scott</asp:HyperLink>
http://localhost/search/scott
16. Advancements in ASP.NET 4.0
Dynamic Data
Enable on any ASP.NET application
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="True"
DataKeyNames="ProductID" DataSourceID="LinqDataSource1">
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext" EnableDelete="True"
EnableInsert="True"
EnableUpdate="True" TableName="Products">
</asp:LinqDataSource>
GridView1.EnableDynamicData (typeof(Product));
DetailsView1.EnableDynamicData(typeof(Product), new {
ProductName = "DefaultName" });
17. Enhancements with ASP.NET 4.0
Web App Deployment
Web Packaging Debug
Web.config transformation
Database deployment
Testing Web.config Release
One-click publish for web applications
Staging
18. Advancements in ASP.Net 4.0
Many more…
Core Dynamic Web App
Ajax Web Forms
Services Data Deployment
Extensible Request
Declarative syntax Web Packaging
Validation
jQuery Persisting Row Selection
Range of allowable URLs Entity templates
Web.config
transformations
Object caching CDN CSS Improvements New Field templates
DB deployment
Extensible HTML, URL,
Creating links
HTTP Header Encoding
Rendering
ScriptManager
Improvements One-Click Publish
Multi-targeting Support M:M (EF)
21. ASP.NET MVC 3.0
Razor View Engine
New view engine (Razor syntax)
Includes IntelliSense and color colorisation for Razor syntax
Can be unit tested
Includes HTML helpers
Chart, WebGrid, Crypto, WebImage, WebMail
23. ASP.NET MVC 3.0
Controller Improvements
Global Action Filters
public static void RegisterGlobalFilters(GlobalFilterCollection
filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new HandleLoggingAttribute());
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
24. ASP.NET MVC 3.0
Controller Improvements
“ViewBag” property
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
“ActionResult” types
HttpNotFoundResult class – HTTP 404
HttpRedirectResult class has boolean “Permanent” property – HTTP 302 or
HTTP 301
public ActionResult Test(int id)
{
return HttpNotFound();
}
25. ASP.NET MVC 3.0
Controller Improvements
JavaScript and Ajax improvements
Client-side validation enabled by default
Remote validator
public class User
{
[Remote("UserNameAvailable", "Users")]
public string UserName { get; set; }
}
28. ASP.NET MVC 3.0
Model Validation Improvements
“DataAnnotations” metadata attributes
“ValidationAttribute” Class
public class User {
[Required]
public string Password { get; set; }
[Required, Compare("Password")]
public string ComparePassword { get; set; }
}
Validation Interfaces
IValidatableObject, IClientValidatable interfaces
Dependency Injection Improvements
29. ASP.NET MVC 3.0
Other new features
NuGet Integration
Partial-Page Output Caching
[OutputCache(Duration = 3600, VaryByParam = "id")]
public ViewResult Details(int id)
{
return View(personRepository.GetById(id));
}
Scaffolding Improvements
30. Resources
Overview of ASP.NET 4 and VS 2010 Web Development
http://www.asp.net/learn/whitepapers/aspnet4#0.2__Toc253429246
Unobstrusive JavaScript
http://en.wikipedia.org/wiki/Unobtrusive_JavaScript
ASP.NET MVC 3
http://www.asp.net/mvc/mvc3#overview
http://msdn.microsoft.com/en-us/library/gg416514(v=VS.98).aspx
ASP.NET MVC 3.0 Service Location (Dependency Injection)
http://bradwilson.typepad.com/blog/2010/07/service-location-pt1-introduction.html
Steven Sanderson’s Blog about MVC Scaffolding
http://blog.stevensanderson.com/category/scaffolding/