Continuing with our MVC 101 series today we look at an important MVC feature, Filters. Filters in ASP.NET MVC are a way to apply cross-cutting logic at the controller level. Some examples of cross-cutting logic is Security and Logging.
Security is a cross cutting concern because, once enabled, we need to apply it for all incoming requests in the web Application. Imagine a world where you don’t have filters, in such case for every request that comes in, you Action method in controller will have to check if the user was Authorized to perform the action and view its result.
This not only violates single responsibility principle of the Action Method (it’s doing two things, evaluating security and computing the Action’s output) but is also an extremely verbose and repetitive work that we’ve to do irrespective of what the Action is supposed to do. Moreover, writing Authentication Code in action method cannot be guaranteed. There is no certainty that a developer may or may not miss out on implementing the code!
Showing posts with label MVC 101. Show all posts
Showing posts with label MVC 101. Show all posts
Getting Started with NUnit in ASP.NET MVC
Visual Studio 2012 comes with a perfectly capable Unit Testing system in MS Test. However if your team’s skills require you to use alternate Testing frameworks like NUnit, Visual Studio is game to play along. In this article, we’ll see the how we can setup NUnit to work with Visual Studio’s Test Explorer and run Unit tests in our project.
1. Download the 2.6.2 (latest at the time of writing) msi installer from http://www.nunit.org. Ensure Visual Studio is not running while the installer is running. This installs NUnit globally along with the NUnit test runner. However the test runner needs .NET 3.5 to run.
2. Another way to include NUnit in your project is to download it using the following Nuget Package Manager command
PM> install-package NUnit
Second part is to setup the NUnit Test Adapter so that Visual Studio recognizes the Test Cases in our project and allows us to Run them from the Test Explorer.
Setting up NUnit
There are two parts to NUnit setup for use in Visual Studio. First part is to install the framework, we can do this in two ways.1. Download the 2.6.2 (latest at the time of writing) msi installer from http://www.nunit.org. Ensure Visual Studio is not running while the installer is running. This installs NUnit globally along with the NUnit test runner. However the test runner needs .NET 3.5 to run.
2. Another way to include NUnit in your project is to download it using the following Nuget Package Manager command
PM> install-package NUnit
Second part is to setup the NUnit Test Adapter so that Visual Studio recognizes the Test Cases in our project and allows us to Run them from the Test Explorer.
Using jQuery Mobile in ASP.NET MVC
This article is from our ASP.NET MVC 101 Tutorial Series
ASP.NET MVC 4 introduced a Mobile Site template that leverages the jQuery Mobile to serve up views tailored for consumption on Mobile devices. Even though the latest ASP.NET site templates have responsive CSS, the mobile views aren’t particularly suited for data intensive views. It would be better if we had views dedicated to handle smaller mobile screens and be mindful of touch based navigation.
You can either create a brand new site specifically targeted for Mobile (if you are starting a green-field project) or you can add Mobile views to an existing MVC project with advanced features like view switching between Mobile and Desktop views.
Today, we are going to look at building a Mobile site ground up, so we’ll start with the mobile site template in Visual Studio
ASP.NET MVC 4 introduced a Mobile Site template that leverages the jQuery Mobile to serve up views tailored for consumption on Mobile devices. Even though the latest ASP.NET site templates have responsive CSS, the mobile views aren’t particularly suited for data intensive views. It would be better if we had views dedicated to handle smaller mobile screens and be mindful of touch based navigation.
You can either create a brand new site specifically targeted for Mobile (if you are starting a green-field project) or you can add Mobile views to an existing MVC project with advanced features like view switching between Mobile and Desktop views.
Today, we are going to look at building a Mobile site ground up, so we’ll start with the mobile site template in Visual Studio
ASP.NET MVC - Using Resource Files to Manage String Constants
This article is from our ASP.NET MVC 101 Tutorial Series
Continuing with the MVC 101 series, today we explore how to save strings in Resource files instead of constants spread out all over the application or worse, hard coded in the source itself.
Mind you, we are not looking at cultures and Internationalization today.
It is very easy to start working on a project and continue adding string in a random, unstructured and adhoc mixture of inline strings and string constants. However, as soon as the marketing team starts to demo the product, you get one change request after the other to update the text/message and error notifications. This is when things go south and you wish you had a central place to store all your string constants.
Hello Resource Files!
Continuing with the MVC 101 series, today we explore how to save strings in Resource files instead of constants spread out all over the application or worse, hard coded in the source itself.
Mind you, we are not looking at cultures and Internationalization today.
It is very easy to start working on a project and continue adding string in a random, unstructured and adhoc mixture of inline strings and string constants. However, as soon as the marketing team starts to demo the product, you get one change request after the other to update the text/message and error notifications. This is when things go south and you wish you had a central place to store all your string constants.
Hello Resource Files!
Adopting ASP.NET MVC enhancements in an Existing Web Forms Project
This article is from our ASP.NET MVC 101 Tutorial Series
Whenever we are starting with a Greenfield project, that is a new project from ground up, we prefer using the latest in the technology stack to the point where the team is comfortable with. However, Greenfield projects are pretty rare; enhancement and feature addition projects, often referred to as brown-field projects, are the way forward for existing applications in an Enterprise. In these projects, we are often saddled with predetermined technologies or frameworks and shoe horning the latest and greatest could mean un-necessary delays and instability. After all ‘don’t fix what’s not broken’ right?
Given this scenario, it’s common to have well established ASP.NET 2.0 Web Forms projects that need enhancements. If the enhancements are independent enough, as in creation of new Screens with new functionality, that is no way going to be ‘embedded’ in existing functionality. We can take a clean break and develop the new functionality in ASP.NET MVC by mixing and matching Web Forms with MVC.
Today we will take a look at the caveats, gotchas and the GUIDs we need to take care to make this possible.
Whenever we are starting with a Greenfield project, that is a new project from ground up, we prefer using the latest in the technology stack to the point where the team is comfortable with. However, Greenfield projects are pretty rare; enhancement and feature addition projects, often referred to as brown-field projects, are the way forward for existing applications in an Enterprise. In these projects, we are often saddled with predetermined technologies or frameworks and shoe horning the latest and greatest could mean un-necessary delays and instability. After all ‘don’t fix what’s not broken’ right?
Given this scenario, it’s common to have well established ASP.NET 2.0 Web Forms projects that need enhancements. If the enhancements are independent enough, as in creation of new Screens with new functionality, that is no way going to be ‘embedded’ in existing functionality. We can take a clean break and develop the new functionality in ASP.NET MVC by mixing and matching Web Forms with MVC.
Today we will take a look at the caveats, gotchas and the GUIDs we need to take care to make this possible.
Custom Templates, Data Annotations and UI Hints in ASP.NET MVC
This article is from our ASP.NET MVC 101 Tutorial Series
Today we look at a feature in ASP.NET MVC that is not really new but immensely useful if we are building ASP.NET MVC applications that lean on the server to do the UI rendering rather than using JavaScript frameworks like KnockoutJS.
Display Templates conceptually are tied to how a particular rendering engine (Razor or Webforms etc.) determines the Type of the model element it is trying to render and then using that information, pick up the Display Template that needs to be used. Along with type, other attributes like Data Annotations and UI Hints are also used to determine the final outcome of our UI.
Today we look at a feature in ASP.NET MVC that is not really new but immensely useful if we are building ASP.NET MVC applications that lean on the server to do the UI rendering rather than using JavaScript frameworks like KnockoutJS.
Display Templates conceptually are tied to how a particular rendering engine (Razor or Webforms etc.) determines the Type of the model element it is trying to render and then using that information, pick up the Display Template that needs to be used. Along with type, other attributes like Data Annotations and UI Hints are also used to determine the final outcome of our UI.
A Simple ASP.NET MVC Demo
Let’s look at these concepts in a little more details using a simple demo app.Unobtrusive jQuery Validation for Knockout in ASP.NET MVC
This article is from our ASP.NET MVC 101 Tutorial Series
In this article, we will explore how we can use the jQuery Validator in an ASP.NET MVC application that uses Knockout Templating. Traditionally, jQuery validator works out of the box with KO ViewModels that are statically bound.
I would recommend reading two articles before you read this one Simple Databinding and Templating using Knockout and ASP.NET Web API & Change Tracking using KnockoutJS and ASP.NET Web API
But there are a couple of additional things that you’ve to do when templates are involved and the DOM is manipulated on the fly.
In this article, we will explore how we can use the jQuery Validator in an ASP.NET MVC application that uses Knockout Templating. Traditionally, jQuery validator works out of the box with KO ViewModels that are statically bound.
I would recommend reading two articles before you read this one Simple Databinding and Templating using Knockout and ASP.NET Web API & Change Tracking using KnockoutJS and ASP.NET Web API
But there are a couple of additional things that you’ve to do when templates are involved and the DOM is manipulated on the fly.
The Harness
Today we use a slightly different harness from the first two parts because we need validations for the dynamic controls added by the template.Areas in ASP.NET MVC is a Useful Feature
Continuing our MVC 101 series, today we look at the concept of Areas. Areas help provide a logical (and physical) separation of the View and Controller layer in ASP.NET MVC.
Areas make use of the Routing mechanism in ASP.NET, so if you are not very clear on how routing works, try this article first.
As we can see, each of these sections are pretty big functionality wise and can be easily segregated into areas. Let’s see how we can get started with Areas.
Areas make use of the Routing mechanism in ASP.NET, so if you are not very clear on how routing works, try this article first.
Working with Areas
Essentially Areas are used for better management of large projects. For example, we may have an ecommerce application that has Admin, Store and Billing areas where Admin manages backend work like creating categories, catalogs, prices etc. Store is the end user portal for searching, selecting adding to cart and Billing is where all invoicing and shipping is managed.As we can see, each of these sections are pretty big functionality wise and can be easily segregated into areas. Let’s see how we can get started with Areas.
What is the AntiForgeryToken and why do I need it? - ASP.NET MVC 101 series
This article is from our ASP.NET MVC 101 Tutorial Series
When we get started with projects, often our core focus is Domain and Business logic. We often slack up on the nitty gritty of securing our web input and sometimes it continues till a disaster strikes and at that point, it becomes a major PR as well as legal headache.
Well today, we will look at a type of security breach in a web application that is called Cross Site Request Forgery or CSRF hack. CSRF is the lesser known cousin of XSS. We will look at XSS in a few days’ time.
You can read more about CSRF on the OWASP site.
When we get started with projects, often our core focus is Domain and Business logic. We often slack up on the nitty gritty of securing our web input and sometimes it continues till a disaster strikes and at that point, it becomes a major PR as well as legal headache.
Well today, we will look at a type of security breach in a web application that is called Cross Site Request Forgery or CSRF hack. CSRF is the lesser known cousin of XSS. We will look at XSS in a few days’ time.
What is Cross Site Request Forgery?
Cross Site Request forgery is a type of a hack where the hacker exploits the trust of a website on the user. In other words, the site trusts the user (because they have authenticated themselves) and accepts data that turns out to be malicious.You can read more about CSRF on the OWASP site.
ASP.NET MVC Cascading Dropdown List
I have been receiving lots of emails from devs requesting me to send them materials to help learn ASP.NET MVC. I usually point them to http://www.asp.net/mvc which is a good starting point. However often when you get started with a project, things are not exactly as in tutorials and you start hitting snags, little things that are different from tutorials and require you to do multiple searches to get their answers to. In essence small issues, already solved, you just need the semantics and gotchas if any.
Starting today, we (Sumit Maitra and Suprotim Agarwal) are starting a MVC 101 series for ASP.NET MVC in which we hope to capture some of these small issues that we see devs who are new into MVC, get stuck with. Today’s example is very simple. We want to save an Address, the State field should get populated after the Country has been selected and it should do it without a full page post back. We use Enterprise Framework DB first with EF Db Context generator (entities will still be POCOs).
Starting today, we (Sumit Maitra and Suprotim Agarwal) are starting a MVC 101 series for ASP.NET MVC in which we hope to capture some of these small issues that we see devs who are new into MVC, get stuck with. Today’s example is very simple. We want to save an Address, the State field should get populated after the Country has been selected and it should do it without a full page post back. We use Enterprise Framework DB first with EF Db Context generator (entities will still be POCOs).
Getting Started
I actually looked up some online databases to see if I could get a list of all countries with their states but the database was upto 250+ MBs. So I settled down with two countries and their states (India and USA).
Subscribe to:
Posts (Atom)