Wednesday, August 17, 2011

Tips & Tricks of ASP.NET MVC3



RAZOR
Problem 1:


Error: 'string' does not contain a definition for 'jpg'

Fix:



Problem 2:

Twitter : @MyTwitterHandle
Error: 'MyTwitterHandle' does not exist in the current context

Fix:

Twitter : @@MyTwitterHandle

Problem 3:

@{
var persons= "Deba, Ramesh, David";
if (persons.Contains("Deba"))
{
Your name is in the list
}
else
{
Your name isn't in the list
}

Error : ; Expected

Fix:

@{
var persons= "Deba, Ramesh, David";
if (persons.Contains("Deba"))
{
Your name is in the list
}
else
{
Your name isn't in the list
}
}


Problem 4:

@{
var message = "Hello Deba";
message
}
Output: Hello Deba

Fix:

@{
var message = "Hello Deba";
@Html.Raw(message)
}
Output : Hello Deba

Patterns
1. Create extension method of URL Helper
2. Use Bootstraper in Global.asax if you have lot of things in Application_Start
3. Decorate your actions with proper AcceptVerbs and CacheControl.
4. Use Depedency Injection
5. Use Typed Views
6. Async Controllers
7. Avoid having huge numbers registrations in Global.asax. Ulternate is- Use Attribute based Route Mapper. http://maproutes.codeplex.com/
8. Use Post-Redirect-Get pattern

[HttpGet]
public ActionResult Employee()
{
return View("Employee");
}
[HttpPost]
public ActionResult Employee(Employee e)
{
if (!Validate(e))
{
return View("Employee", e);
}
RedirectToAction("AddEmployee");
}

9. No Magic Strings

Using expression based Form generation - Html.TextBoxFor(order=>order.Customer.Name)
Using expression based URL generation - Url.Action(c=>c.Index())
Using expression based RedirectToAction - RedirectToAction(c=>c.Index())

10. Keep your controller free from HttpContent. If you require to use it, then use custom action filter or interface or wrapper.
10. Reusing view logic by using @helper.
Step1 - Create a view in App_Code folder and write all your helper methods.
Ex:

@helper FormatCurrency(Decimal price)
{
@price.ToString("c")
}

Step 2 - Calling the helper method

@FormatCurrency(product.price)


Issue with @helper - Built-in Html helper methods are not available inside the views in App_Code. Fix - http://blogs.msdn.com/b/davidebb/archive/2010/10/27/turn-your-razor-helpers-into-reusable-libraries.aspx


Related Topics -
MVC 3 Tools Update


ASP.NET MVC WebGrid

Friday, August 12, 2011

IIS 7.0 throws the error HTTP Error 503. The service is unavailable

Issue : Default Web Site throws HTTP 500 error. The Service is unavailable.

Cause : Port 80 has been reserved for a named account using netsh command netsh http add urlacl=http://+:80/ user=<>. Port reservation is done to give exclusive access to a particular account on resource such as http port or url. To check list of all reserved port urls, use following netsh command.
netsh http show urlacl

Fix : Remove the port 80 reservation by using following netsh command.
netsh http delete urlacl url=http://+:80/

Thursday, August 11, 2011

IIS related services

From August 12, 2011

Wednesday, August 10, 2011

Publishing WCF Services using IIS and .NET 4.0

1.0 Run the command aspnet_regiis -i to register asp.net 4.0 with IIS, updates both the classic and integrated mode handlers and script mappings in the IIS metabase.
1.1. Open Visual Studio in Run as Administration mode.
2. Right click on the WCF Service Project and choose Publish.
3. In the Publish Web Site Dialog box, choose following settings -
3.1 Publish Method: Web Deploy
3.2 Service Url - Address of the Service through which the publishing happens.
Ex: publishing locally - localhost
3.3 Site/Application - Url of Publish To site Ex: Default Web Site/MyApp. Note - This site should be already created in IIS as an application.
4. By Default the service will be deployed to run as Default App Pool which uses .NET 2.0. and will fail. Therefore, you need to change the App Pool of this Web Site to ASP.NET V4.0 Application Pool.
5. Ensure the Account used for the Application Pool has necessary permission on the resources used by WCF Service like DB Access etc.

Friday, August 5, 2011

Project Management Tips


Situation - What action will you take to achieve code quality ?
1. Keep language specific code review guidelines and Naming Conventions document handy for the developer to refer before he writes any single line of code.
2. Peer Review
3. Code Analysis Tools. Ex: VS2010 Code Analysis, StyleCop, FxCop, NDepend, SPDispose Checker.
4. Code should be unit tested using Tools like NUNIT, Visual Studio Testing Framework.
Situation - Developer unable to deliver as per the estimate/plan.
1. Insufficient training may be one of the issue. What type of training is required ? Domain Knowledge or Technical.
2. Attach a senior resource/domain analyst to guide the developer in his work.
Situation - Large number of UAT issues.
1. Revisit your system test cases. They may not cover the complete functional testing.
2. Engage a Domain Analyst to review the system cases.
Situation- Unplanned leave schedule of the Team is impacting delivery
1. The first activity that a PM should take up at the start of the project is to maintain a leave calendar for the team in a shared location. Team member should update their leave plan well in advance for better planning.
Situation - What is the best way to manage bugs in the System ?
1. Identify a tool to manage bugs or issues.
2. Classify the bugs as per their severity i.e High, Medium and Low.
3. Assignment of bugs to resources as per their severity/complexity.
4. Initial Estimation of bugs/issues by resources.
5. Actual Estimation of bugs/issues by resources.
6. Set the status of the bug i.e. Unable to Reproduce, Code Complete, Closed, Open, Waiting etc. with proper comments.
Situation - Re-open Bugs
1. Root cause analysis.
2. Insufficient Test cases.
Situation - Client has the option to change the requirement anytime during the development.
1. Ensure you have configured your Repository (SharePoint etc.) to send alert whenever there is a change in requirement.
2. Analyze and do an impact analysis as the next step. Artifacts can be impacted - Source Code, Unit Test Cases, System Test Cases, Build Script etc.
3. Impact on estimation
4. Communicate the Client about the impact
5. Implementation of the change of requirement.
Situation - Code coverage/review/refactoring is missing during project execution
1. This is a common problem in software projects. The team miss some important activities like code review/unit testing/refactoring etc. under delivery pressure. There is a need for monitoring the process during the execution and enforce the process to ensure quality delivery. Escalate the issue.

Risk management breakdown in software projects -