Some folks may keep falling into a trap and are unaware of what they are doing. This post highlights the Dos and the Don’ts in general regarding ASP.NET. The majority of the items listed are taken from the ASP.NET team recommendations. Though this may not be a complete checklist, it covers some of the most common “gotchas” folks run into.
If you know other tips about the Dos and Don’ts in ASP.NET in general, feel free to drop a comment so I can update the list. :)
If some of you are still using Control Adapters, especially those WebForms folks - you should avoid it, as much as possible.
Avoid: Control Adapters, as these were created to support mobile controls rendering different markups for different devices.
Prefer: CSS media queries, responsive design and mobile specific views.
Style Properties on Controls
- Try to Avoid: The four thousand specific control style properties, e.g.
- Using inline CSS styles, e.g.
- Prefer: CSS stylesheets. You can roll your own or use Bootstrap or a combination of your own CSS.
- Try to Avoid: Mixing your jQuery code, or other JS frameworks code with WebForm's AjaxControlToolkit controls to avoid functionality issues.
- Prefer: Stick to the specific control libraries.
- Do Not: Over use it (Think about performance and maintainability)
- Do: Use it, when necessary and if it makes sense to use it.
- Try to Avoid: It doesn’t help you to become a better web developer.
- Prefer: AJAX e.g jQuery AJAX can be used to do asynchronous updates.
Page and Control Callbacks
- Try to Avoid: Page callbacks or control callbacks.
- Prefer: Anything else, e.g. Page Method, Web Service, AJAX, Web API.
Scripts and CSS Files
- Do: Minify, bundle your CSS and Script files when deployed on production.
- Try to Avoid: Deploying unminified scripts and CSS when you can minify them.
Static Script References
- Try to Avoid: Referencing local script references (e.g jQuery)
- Prefer: Use CDN (Content Delivery Network), when referencing is done on static files
- But Always: Do a fallback local reference, in case CDN fails
- Try to Avoid: BrowserCaps, as it has a history of breaking as new browser versions are released
- Prefer: Client-side feature detection and lightup, such as via Modernizr.
This article highlights the prevention of SQL Injection. Protect Your Data: Prevent SQL Injection
Displaying of Data
- Do Not: Display huge amounts of data in your page as it can affect the performance of your App, and it is not user-friendly.
Do: Limit the amount of data to be displayed.
(1) Filter out items and load the associated data.
(2) Apply paging (e.g using custom paging with LINQ or using SQL paging).
(3) Apply data caching (but be careful: use it only where it makes sense).
- Do Not: Depend on the request validation to protect your site against XSS attacks.
(1) Validate well-formedness of the data
"Is this user-submitted value, a valid System.Uri whose scheme is http: or https:?"
(2) Encode data on the way out.
CSHTML: @foo ? ASPX: <%: foo %>
Cookieless Forms Auth& Session
For the developers who use ASP.NET runtime < 4.5.2
- Do Not: Set EnableViewStateMac = false
Not Ever: Not even on a single page.
"But I’m not using ViewState!" is not a valid excuse.
Do: Tease Microsoft for even allowing this as an option in the first place. :D
Well MS already forbids it when they released ASP.NET 4.5.2.
- Do Not: Use
<appSettings> to disable Microsoft security fixes for any lengthy time in production.
- Do: Use Microsoft security-sensitive
<appSettings>config as temporary compatibility that shims while rolling out Server upgrades or patches.
Consult the list here.
- Try to Avoid: Registering for these events from within managed
- Prefer: Using native IIS modules, if you need to hook these asynchronous pipeline events.
Asynchronous Page Events
- Try to Avoid: Writing async
void methods [like
Page_Load] for page life-cycle events.
- Prefer: Using
Page.RegisterAsyncTask() instead, if you need to register asynchronous work.
- Do: Set <httpruntimetargetframework=“4.5”> if using
Response.Redirect & End
Response.End(), which aborts the current thread in synchronous requests and halts the code execution.
For asynchronous handlers,
Response.End() does not abort the current thread, so code execution continues.
If you need to redirect the response, use the method appropriate for the framework you’re using. For example, in MVC, return a
RedirectResult instead of calling
EnableViewState & ViewStateMode
Be Aware: Replaced by the UniversalProviders and ASP.NET identity which works with all the databases and the Entity Framework supports including SQL, Azure SQL, SQL Compact, MySQL and more.
- Try to Avoid: Session, as ASP.NET will forcibly release the session object lock at a potentially inopportune time Blocking I/O operations.
- Prefer: WebSockets, if possible, has much lower per-request memory overhead.
- Try to Avoid: The traditional way of pinging the Server for the updates with AJAX.
- Prefer: ASP.NET SignalR. It provides a simple and clean API that allows you to create real-time web apps. HTML5 is also worth trying.
- Do Not: Stick to use Web Forms.
(1) Learn other technologies that ASP.NET offers such as MVC, Web API, SignalR and ASP.NET Mobile.
Also check out the new ASP.NET Core.
(3) Keep up to date with what’s the latest and know the new features it offers.