I will try to show you a basic feature of ASP.NET MVC, creating custom HTML Helpers. We can reduce the amount of logic in view pages (razor or asp.net –aspx- pages) by extracting logic code as method.

HTML Helper methods are nothing more than methods that are returning string. If you want to write your own Html Helper method you have to write extension methods for HtmlHelper class. ASP.NET MVC Framework itself contains extension methods for HtmlHelper class to have well structured helper methods separation. ASP.NET MVC Framework Html Helper extension methods are located under System.Web.Mvc.Html namespace.

How to write your own Html Helper methods?

Razor pages uses System.Web.Mvc.WebViewPage type as base class. Razor pages automatically inherits from this type, If you want to see the configuration or if you want to replace it with your base class you should check web.config file in your ASP.NET MVC project. This subject is not in this article’s context but if you want to use your own base class, you can create a new class which inherits from WebViewPage and you can edit web.config file with your type name.

Read the rest of this entry »

If you are working in a RESTful project or an ajax based web application probably you will need to return http error codes with custom content. Like returning a custom XML message from your endpoint with a specific (like 400 BadRequest) HTTP Status Code.

[HttpPost]
public ActionResult Create(Customer customer)
{

    var validationResult = _validator.Validate(customer);

    if (validationResult.HasErrors)
    {
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return CreateXmlContentActionResult(validationResult.ToXml());
    }


    //do magic here

    return CreateXmlContentActionResult(CustomerCreateSuccesMessage);
}


protected ActionResult CreateXmlContentActionResult(string xmlData)
{
    return Content(xmlData, "text/xml");
}

IIS 7 has an option named keeping existing response content. This option specifies what happens to an existing response when the HTTP status code is an error. For enabling custom content for http error status codes you need to change your web.config file. Under system.webServer element

<system.webServer>

    <httpErrors existingResponse="PassThrough"></httpErrors>

</system.webServer>

Read the rest of this entry »

 

Two weeks ago we had a memory leak problem with Silverlight Toolkit Chart Control. If you want to see a sample code for reproducing and more details you can read details of the issue on CodePlex. After spending some time with this Silverlight Toolkit Chart Control Memory Leak problem, I figured out the reason of memory leak.

Chart control has a weak reference implementation but if you add or remove members after first initialization of ItemSource of LineSeries object it doesn’t invoke the weak reference detaching logic. I don’t remember all base class and method names in SilverLight Toolkit but after spending one day with WinDBG, Silverlight Spy and source code of Silverlight Toolkit I found the problem.

Solution is very easy: instead of adding and removing members to existing ObservableCollection. Create a new instance of ObservableCollection and set ItemSource everytime you need to update datasource.

If you are using ReSharper, you know ReSharper can detect inconsistent namings in your code files. That’s a wonderful feature like other ReSharper features. But default settings is not good for test files because I am using a naming standard with underscores and ReSharper warns me for inconsistent name.

naming_style_for_test_before

If you press Alt+Enter ReSharper will suggest you a consistent name by checking your settings.

But If you are using this kind of naming for your test methods you can disable this warning for your test files.

Read the rest of this entry »

I needed to export some data as CSV file. And I decided to create an action result to return correct format. In my scenario DataTable was the best option to use as a data source.

This code snippet shows how to export a DataTable as CSV file. As you know this file format can be opened with Excel like an Excel document.

Here is the code snippet that I used.

Read the rest of this entry »

Sharepoint 2010 version can be installed on client operating systems. (Windows 7,Vista)

I will share some information about installing Sharepoint Foundation on a Windows 7 without AD (Active Directory) (Domain). So this blog entry doesn’t aim to be full installation guide for Sharepoint (Foundation) 2010.

Before starting to installation you need to install a hotfix for your operating system. The hotfix is available for Windows Server 2008 Service Pack 2, Windows Vista Service Pack 1, and Windows Vista Service Pack 2 and for Windows Server 2008 R2 and Windows 7.

After you downloaded SharepointFoundation.exe from Microsoft download center, you need to extract files out from the exe file.

SharepointFoundation.exe gets “extract” parameter to extract files. You can run the command at the below to extract files.

Read the rest of this entry »

I am using ASP.NET MVC framework from the time first beta bits released, I am big fan of this framework because of it’s extensibility and specifically simplicity.

ASP.NET MVC framework activates the controllers when a request arrived to it’s pipeline. Default naming and structure rules of controllers are :

  • Controller class name must ends with “Controller”.
  • Controller class must have constructor with no parameters.

These constraints come from the default controller factory of ASP.NET MVC Framework.

Why I need to create a custom controller factory?

It doesn’t have a meaning to create because of you can create that. You may need to create your controllers in a special way like we are doing in this post, you may want to pass some parameters on constructor of controllers. You may want to manage the lifetime of objects required by controller constructors with an IoC library (like windsor, structuremap).

It’s a good practice to passing required Repository objects on constructor, so you can pass actual mocked or stub objects in the unit tests, plus, in runtime your repository objects lifetime can manage by your IoC library.

Read the rest of this entry »

SQL Server database snapshot feature provides you to have a read-only copy of your current database. You can create a snapshot only on the server which is actual db located. In other words, you can’t create a snapshot on any other server than the server that host your actual database.

We use snapshots for solving some reporting problem and some scenarios like that. Benefits of the snapshot is not the subject of this blog entry.

If you have experience with SQL Server snapshot, did you think about this limitation? What is the design issues under this limitation?

Read the rest of this entry »

Dynamic Management Views and Functions feature comes with SQL Server 2005 version. They provides some information about system, and what is going under when SQL Server running. So you can use DMV and DMF objects to monitor the system and tune it. These object located under sys schema and names are start with the dm. As previous system objects in SQL Server, internal implementation of these objects can be change with the future releases.

I want to share some code snippets about some of DMV/F’s which I had use while tuning and monitoring SQL Server.

Read the rest of this entry »

Continuous Integration is a software development practice. This practice and name firstly pronounced by Martin Fowler and Kent Beck in 1999. The idea behind this practice is integrating the code that is checked in by developers frequently.

Picture this scenario; you have a team of developers and every developer makes checkins their work. All of the developers try to build the changes they made successfully in their own machines. But every developer makes checkins and these checkins must be integrated and you need to know that all checkins can work in the whole system successfully. I know some companies dedicate some people only for this job -making integrations manually-. This is not a good solution.

The solution is automation! Continuous Integration (CI) automates this process.

Read the rest of this entry »

top