Community Blogs

Browse by Tags

Related Posts

  • ASP.NET MVC Tip #24 – Retrieve Views from Different Folders

    In this tip, I demonstrate how you can retrieve a view from any folder in an ASP.NET MVC application. I show you how to use both specific paths and relative paths. Until today, I thought that a controller action could return a view from only one of two places: · Views\ controller name · Views\Shared For example, if you are working with the ProductController, then I believed that you could only return a view from either the Views\Product folder or the Views\Shared folder. When looking through the source code for the ViewLocator class, I discovered that I was wrong. If you supply a “Specific Path” for a view, you can retrieve a view from any location in an ASP.NET MVC application. The ProductController.Index() action in Listing...


  • ASP.NET MVC Tip #23 – Use POCO LINQ to SQL Entities

    In this tip, I demonstrate how you can create LINQ to SQL entities that do not contain any special attributes. I show you how you can use an external XML file to map LINQ to SQL entities to database objects. I’ve talked to several people recently who are deeply bothered by the fact that the LINQ to SQL classes generated by the Visual Studio Object Relational Designer contain attributes. They want to take advantage of the Object Relational Designer to generate their entity classes. However, they don’t like the fact that the generated entities are decorated with a bunch of attributes. For example, if you use the Object Relational Designer to generate a LINQ to SQL class that corresponds to the Movies database table, then you get the...


  • ASP.NET MVC Tip #22 -- Return a View without Creating a Controller Action

    In this tip, I demonstrate how you can eliminate controller methods that simply return views. I show you how to use the HandleUnknownAction method to handle every request against a controller automatically. I saw Phil Haack use the following tip in a demo that he presented. I thought that it was such a great idea that I had to share it. There is no good reason to write code unless there is a good reason to write code. I discover that I write a lot of controller actions that do nothing more than return...( read more ) Read More...


  • ASP.NET MVC Tip #21 – Fake the Data Context

    In this tip, I demonstrate how to create an in-memory data context class that you can use when unit testing ASP.NET MVC applications that access a database. In this tip, I explain how you can write unit tests for data access code within an ASP.NET MVC application. I demonstrate how to unit test the LINQ to SQL DataContext without using a Mock Object Framework. First, I show you how to create a generic DataContextRepository that can be used to retrieve and modify database records. Next, I show you...( read more ) Read More...


  • ASP.NET MVC Tip #20 – How to Unit Test Data Access

    In this tip, I demonstrate how you can write unit tests for MVC controller actions that access a database. I show you how to create unit tests for your LINQ to SQL controller action code. Most ASP.NET MVC applications that I write contain a substantial amount of data access code. Typically, I use Microsoft LINQ to SQL to perform database operations. How do you unit test this data access code? There are several different approaches that you might take to this problem: (1) Don’t unit test data...( read more ) Read More...


  • ASP.NET MVC Tip #19 – Use the nVelocity View Engine

    In this tip, I demonstrate how you can use the nVelocity view engine, instead of the normal Web Forms view engine, when displaying views from an ASP.NET MVC application. By default, you build views for an ASP.NET MVC application by creating ASP.NET Web Form pages (.aspx files). You are not required to do this. If you prefer, you can swap out the Web Forms view engine and use an alternative view engine. In this tip, I demonstrate how you can use the nVelocity view engine. Why would you want to use...( read more ) Read More...


  • ASP.NET MVC Tip #18 – Parameterize the HTTP Context

    Context is the enemy of testability. In this tip, I demonstrate how you can eliminate, once and for all, the HTTP Context from an ASP.NET MVC application. A controller action that interacts only with the set of parameters passed to it is very easy to test. For example, consider the following simple controller action: VB.NET Version Public Function InsertCustomer( ByVal firstName As String , ByVal lastName As String , ByVal favoriteColor As String ) As ActionResult CustomerRepository.CreateCustomer...( read more ) Read More...


  • ASP.NET MVC Tip #17 – How to Run an ASP.NET MVC Application

    In this tip, I explain the different options for running an ASP.NET MVC application from Visual Studio 2008. I recommend that you run an ASP.NET MVC application directly from the ASP.NET Development Web Server. An ASP.NET MVC application works differently than a normal ASP.NET Web Forms application. When you request a URL, there might not be a corresponding page on your hard drive. For example, imagine that you request the following URL: /Product/Index.aspx There is no reason to assume that an ASP...( read more ) Read More...


  • ASP.NET MVC Tip #16 – Create ASP.NET MVC Macros

    In this tip, I show you how you can create a Visual Studio 2008 macro that creates a new MVC controller, view folder, and controller unit test with a single command. Don’t get me wrong. I like the Visual Studio 2008 designer tools. I like dragging-and-dropping items from the toolbox. I’ve memorized many useful Visual Studio keyboard shortcuts. But, at the end of the day, there is nothing faster than firing off a quick command from the Command window. In this tip, I explain how you can...( read more ) Read More...


  • ASP.NET MVC Tip #15 – Pass Browser Cookies and Server Variables as Action Parameters

    In this tip, I demonstrate how you can pass browser cookies and HTTP server variables to controller action methods in the same way as you can pass form and query string parameters. Imagine that you make the following browser request against an ASP.NET MVC web application: http://localhost/Product/Index When you make this request, by default, the ASP.NET MVC framework will invoke an action named Index() exposed by a class named ProductController. There is a class in the ASP.NET MVC framework, named...( read more ) Read More...


Page 1 of 6 (59 items) 1 2 3 4 5 Next > ... Last »
Page view counter