Archive

Archive for the ‘MVC’ Category

Razor vs WebForm(aspx)


RAZOR Web Form(ASPX)
Razor View Engine is an advanced view engine and introduced with MVC3. This is not a language but it is a mark-up syntax. ASPX View Engine is the default view engine for the ASP.NET MVC that is included with ASP.NET MVC from the beginning.
Namespace:System.Web.Razor. Namespace: System.Web.Mvc.WebFormViewEngine
Extension:.cshtml with C# or

.vbhtml with VB extension for views, partial views, editor templates and for layout pages.

Extension:.aspx extension for views

.ascx extension for partial views & editor templates and .master extension for layout/master pages.

RAZOR is much easier and cleaner than Web Form. It uses @ symbol in coding.
e.g.@Html.ActionLink(“link”, “click”)
ASPX Uses <% and %> delimiter in coding.
e.g. <%: Html.ActionLink(“link”, “Click”) %>
RAZOR engine comparatively slow but provides better security than ASPX. Razor Engine prevents XSS attacks(Cross-Site Scripting Attacks) means it encodes the script or html tags like <,> before rendering to view. Web Form is comparatively faster but less secure than RAZOR. Web Form Engine does not prevent XSS attacks means any script saved in the database will be fired while rendering the page
Razor Engine, doesn’t support design mode in visual studio means you cannot see your page look and feel without running application. Web Form engine support design mode in visual studio means you can see your page look and feel without running the application.
Razor Engine support TDD (Test Driven Development) since it is not depend on System.Web.UI.Page class. Web Form Engine doesn’t support TDD (Test Driven Development) since it depend on System.Web.UI.Page class which makes the testing complex.

 

MVC: From where to find System.Web.MVC dll in a system

April 6, 2015 2 comments

Default path for System.Web.MVC dll

MVC 2
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 2\Assemblies\System.Web.Mvc.dll

MVC 3
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll

MVC 4
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Web.Mvc.dll

MVC 5
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Stack 5\Packages\ Microsoft.AspNet.Mvc.5.0.0\lib\net45\System.Web.Mvc.dll

Hope this help !

Bundling and minification in MVC3 and Asp.Net 4.0

March 6, 2013 1 comment

Adding references:

First of all add the references of System.Web.Optimization.dll and WebGrease.dll to your MVC3 and Asp.Net 4.0 projects as shown below. You can download the dll by using download link.


Creating Bundle:

Now create the bundle for your css and js files with in the Global.asax file as shown below.

Here, I have created the bundle of all required css and js files. You can also add your own css and js files with complete path using Include method.

Registering Bundle:

Adding Bundles to Layout Page in MVC3:

Now you can add the above created style and script bundles to the Layout page or where you want to use as shown below:

Adding Bundles to Master Page in Asp.Net 4.0:

Now you can add the above created style and script bundles to the Master page or where you want to use as shown below:

In Asp.Net 4.0 you also required to add System.Web.Optimization namespace and assembly Microsoft.AspNet.Web.Optimization.WebForms reference to the web.config file of your Asp.Net 4.0 project as shown below:

You need not to do any changes in web.config file of your MVC3 project.

Enabling Bundling and Minification in debug mode

Bundling and minification doesn’t work in debug mode. So to enable this features you need to add below line of code with in Application_Start event of Global.asax.

How it works..

Now run your application and you will see that all the css and js files are converted to single css and js file as shown below:

Minification

Minification is technique for removing unnecessary characters (like white space, newline, tab) and comments from the JavaScript and CSS files to reduce the size which cause improved load times of a webpage. There are so many tools for minifying the js and css files. JSMin and YUI Compressor are two most popular tools for minifying the js and css files. Use these tools for minifiying your css and js files and use in your application with “.min” suffix. So that you can easily identified that this is a minimize version of your css or js file.

Hope it helps!

Advanced Options of ASP.NET Bundling and Minification


Rick Anderson has written a great introduction to ASP.NET Bundling and Minification at here. In addition to the options presented by Rick, there are some other features in System.Web.Optimization framework which is also important for developers to understand in order to make their application performance and architecture good. In this article, I will show you some of the advance stuff of ASP.NET Bundling and Minification.

Description:

Server.MapPath is one of the most used method in ASP.NET. Lot of libraries/assemblies has used this method. The problem of using this method inside a library  is that it will tie the users of this library with ASP.NET intrinsic Server object which makes unit testing a little difficult. Fortunately, System.Web.Optimization have a special property called BundleTable.MapPathMethod which allows users to provide a fake method for unit testing.

There are some script files that you will never expect to add on a web page, for example, *.intellisense.js and *-vsdoc.js script files which are used by Visual Studio for intellisense. There are some other scripts files that you need to ignore during production, like, *.debug.js file. There are some other script files that you will not add during debugging/testing, for e.g, *.min.js and *.min.css files. Luckily, our optimization library will automatically takes care of these situation through BundleCollection.IgnoreList property. Here is the what this property contains by default,

You can easily override this by using BundleCollection.IgnoreList property in BundleConfig.cs file. Note: it is also important that you should be aware about a bug in the implementation of this.

The ordering of scripts and css files is also very important. For example, if you have jquery.js and jquery.ui.js then jquery.js file should be appear before jquery.ui.js. Similarly for css, reset.css should appear first(if exist) and then normalize.css(if exist) and then the remaining ones. Fortunately again, our optimization assembly will also handle this scenario by using BundleCollection.FileSetOrderList property. You can also override this inside BundleConfig.cs file. Here is the what this property contains by default,

Now let’s say that you have only registered the jquery-1.6.2.js(not jquery-1.6.2.min.js). The optimization framework will still pick the jquery-1.6.2.min.js instead of jquery-1.6.2.js during production. This is possible via BundleCollection.FileExtensionReplacementList property. Like others, you can also override this inside BundleConfig.cs file. Here is the this property contains by default,

For clearing all these settings, you just need to call BundleCollection.ResetAll method. This method will clear all the above lists and empty the registered bundles.

The System.Web.Optimization framework also allows you to define a custom orderer for a bundle. You just need to implement the IBundleOrderer.OrderFiles method in a new class and set the Bundle.Orderer property. Here is an example.

Categories: MVC

Understanding asp.net MVC (Model View Controller) architecture

November 28, 2012 2 comments

This article is intended to provide basic concept and fundamentals of asp.net MVC (Model View Controller) architecture workflow for beginners.

Introduction:

“M” “V” “C” stands for “MODEL” “VIEW” “CONTROLLER” , asp.net MVC is an architecture to develop asp.net web applications in a different manner than the traditional asp.net web development , web applications developed with asp.net MVC is even more SEO (Search Engine Friendly ) friendly.

Developing asp.net MVC application requires Microsoft .net framework 3.5 or higher.

MVC interaction with browser:

Like a normal web server interaction, MVC application also accept request and respond web browser same way.


Inside MVC architecture:

Whole asp.net MVC architecture is based on Microsoft .net framework 3.5 and in addition uses LINQ to SQL Server.

What is a Model?

  1. MVC model is basically a C# or VB.net class
  2. A model is accessible by both controller and view
  3. A model can be used to pass data from Controller to view.
  4. A view can use model to display data in page.

What is a View?

  1. View is an ASPX page without having a code behind file
  2. All page specific HTML generation and formatting can be done inside view
  3. One can use Inline code (server tags ) to develop dynamic pages
  4. A request to view (ASPX page) can be made only from a controller’s action method

What is a Controller?

  1. Controller is basically a C# or VB.net class which inherits system.mvc.controller
  2. Controller is a heart of whole MVC architecture
  3. Inside Controller’s class action methods can be implemented which is responsible for responding to browser OR calling view’s.
  4. Controller can access and use model class to pass data to view’s
  5. Controller uses ViewData to pass any data to view


MVC file structure & file naming standards

MVC uses a standard directory structure and file naming standards which is very important part of MVC application development.

Inside the ROOT directory of the application there must be 3 directories each for model, view and Controller.

Apart from 3 directories there must have a Global.asax file in root folder. And a web.config like a traditional asp.net application.

  • Root [directory]
    •  Controller [directory]
      • Controller CS files
    •  Models [directory]
      • Model CS files
    •  Views [directory]
      • View CS files
    • Global.asax
    • Web.config

Asp.net MVC Execution life cycle

Here is how MVC architecture executes the requests to browser and objects interactions with each other.

A step by step process is explained below: [Refer figure as given below]


Step 1: Browser request

Browser request happens with a specific URL. Let’s assume that user entering URL like: [xyz.com]/home/index/

Step 2: Job of Global.asax – MVC routing

The specified URL will first get parsed via application_start() method inside Global.asax file. From the requested URL it will parse the Controller, Action and ID.

So for [xyz.com]/home/index/:

Controller = home

Action = index()

ID = empty — we have not specified ID in [xyz.com]/home/index/, so it will consider as empty string

Step 3: Controller and Action methods

MVC now find the home controller class in controller directory. A controller class contains different action methods,

There can be more than one action method, but MVC will only invokes the action method which is been parsed from the URL, its index() in our case.

So something like: homeController.index() will happen inside MVC controller class.

Invoking action method can return plain text string OR rendered HTML by using view.

Step 4: Call to View (ASPX page)

Invoking view will return view() . a call to view will access the particular ASPX page inside the view directory and generate the rendered HTML from the ASPX and will respond back to the browser.

In our case controller was home and action was index(). So calling view() will return a rendered HTML from the ASPX page located at /views/home/index.aspx.

This is it, the whole process ends here. So this is how MVC architecture works.