Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Identify ASP.NET MVC Assembly Version

DZone's Guide to

Identify ASP.NET MVC Assembly Version

In this post, we'll learn to check our ASP.NET MVC version installed in our system using C# during runtime and manually customize an error page in ASP.NET/ ASP.NET MVC 5.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Introduction:

The MVC  pattern separates an application into three main parts.

  1. The Model − Collection of classes that are working with the business logic.
  2. The View − It is responsible for the User Interface. It is an HTML format whose extension is .cshtml.
  3. The Controller − Collection of classes that are working data logic and business logic.

Description:

Every MVC application contains 3 folders: Controller, View, and Model.

Every controller contains controller action methods which are responsible for creating the view to transferring data from the controller to view through ViewData, ViewBag, and TempData.

Every model contains some properties and by using this model class name reference while using namespaces inside a controller we can access related properties of classes for business logic implementation.

In the case of the entity data model creation, an edmx file automatically creates a model class based on the table name or related database object name and with properties related to the column name of that table.

The Way to Find Assembly Version Without Using Code, Called Design Time

In the solution explorer, expand the "References" folder. Right-click on the System.Web.Mvc assembly and select "Properties" to can find the version.

Steps to Be Followed at Runtime Using Code:

Step 1:

Create a controller named “Home.”

Add the code given below in HomeController.cs for better results, as expected. 

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.Mvc;  
namespace Mvc_Version.Controllers {  
    public class HomeController: Controller {  
        //  
        // GET: /Home/  
        public string Version() {  
            return "<h2>The Installed Mvc Version In your System Is : " + typeof(Controller).Assembly.GetName().Version.ToString() + "</h2>";  
        }  
    }  
}  

Code Description:

We mentioned the controller action method version in HomeController.cs.

For this, we should change the action method name in RouteConfig.cs for better loading of the page. For the first time, we will make it a start page.

Add the highlighted code in your file, RouteConfig.cs. 

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
    using System.Web.Mvc;  
    using System.Web.Routing;  
    namespace Mvc_Version {  
        public class RouteConfig {  
            public static void RegisterRoutes(RouteCollection routes) {  
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
                routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {  
                    controller = "Home", action = "Version", id = UrlParameter.Optional  
                });  
            }  
        }  
    }   

Output

The expected output is shown below.

http://localhost:49952/Home/Version

Home - Controller name

Version - Controller action method

Image title

Summary:

  • Installed ASP.NET MVC Assembly Version using C#

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
c# ,mvc ,visual studio 2015 ,web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}