Tag: ASP.NET MVC

4 Best Practices for ASP.NET MVC Application

There are plenty of best practices for ASP.NET MVC Application which can be followed by the ASP.NET MVC Project developer. In this blog post , we will cover 4 of the best practices when developing ASP.NET Web Applications. 4 Best Practices for ASP.NET MVC Application 1. Request Validation The Request Validation is one of the features in ASP.NET MVC which is enabled by default and…

Providing Default Values for a Route in ASP.NET MVC

The default values are useful specially when the entered URL does not contain the items with in the pattern . In these scenarios , the URL can route to the default action or controller. Providing Default Values for a Route in ASP.NET MVC Below is a sample code snippet demonstrating the usage of the default values to the route. In the above example , if…

Retrieve Data from the Request Object in ASP.NET MVC

In ASP.NET MVC, the Request property defined within the controller can be used to retrieve the data about the request. They can be accessed easily in the controller. Some of the useful properties within the Request objects includes Request.QueryString – This is a NameValueCollection which can be used to access variable sent with this request using GET method. Request.Form – This is a NameValueCollection will…

How to Create and Register a Route in ASP.NET MVC ?

The routes for the ASP.NET MVC Web Application is defined in the RouteConfig.cs under the App_Start folder of the ASP.NET MVC 4 Project. The RouteConfig.CS contains the following code. The RouteConfig contains a static RegisterRoutes function which is called from the Global.asax file. The above method in the Global.asax is triggered when the application is started for the first time. How to Create and Register…

How to retrieve the URL of the current Page in ASP.NET MVC 4 Controller ?

If you want to retrieve the URL of the current Page in a ASP.NET MVC 4 Controller , you can get it by using the AbsoluteUri property defined in the Request.Url class. How to retrieve the URL of the current Page in ASP.NET MVC 4 Controller ? using System; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication1.Controllers { public class EmployeeController : Controller { //…

Difference between RenderBody, RenderPage and RenderSection in ASP.NET MVC

Difference between RenderBody, RenderPage and RenderSection in ASP.NET MVC The RenderBody can generally be found in the Layout page which is a kind of master page. Each Layout page can contain only one RenderBody method. It is like a Content Placeholder in ASP.NET WebForms. With the RenderPage, the content of the page can be filled by other pages. This takes the physical path of the…

Specifying Text for Html.LabelFor in ASP.NET MVC Application

The Html.LabelFor helper method can be used to display label in the ASP.NET MVC View . The LabelFor takes the parameter of the property defined in the model to display the text . Specifying Text for Html.LabelFor in ASP.NET MVC ApplicationThe developers can use the DisplayAttribute for the property which is used in the System.ComponentModel.DataAnnotations namespace . For example , is the property name in…

IHttpHandler.IsReusable in ASP.NET MVC

The IHttpHandler includes the IsReusable property which is generally to used to retrieve the value which indicates if another request can use IHttpHandler or not. In simple terms , the property IsReusable get the value that indicates if the multiple requests can be processed with the same IHttpHandler instance.

Multiple types were found that match the controller Error in ASP.NET MVC

There are times when you get the error similar to the below message when you type the URL “Multiple types were found that match the controller named ‘Employee’. This can happen if the route that services this request (‘Abundantcode/{controller}/{action}/{id}’) does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of…

.cshtml and .aspx file in ASP.NET MVC

When developing the ASP.NET MVC Web Application , you would have observed that there are 2 different View Engines that are available by default . These are called Aspx View Engine Razor View Engine When the view is created with the Razor View Engine , the file will have the extension .cshtml whereas the View created with the ASPX View Engine will have the .aspx…

How to find if the request is from Ajax in ASP.NET MVC?

It is easy to find out if the actual request came from Ajax or is it a normal request in ASP.NET MVC. In the controller, one can use the Request.IsAjaxRequest method to find this. How to find if the request is from Ajax in ASP.NET MVC? Below is a sample code snippet demonstrating how to find if the request is from Ajax in ASP.NET MVC?

UrlHelper.GenerateUrl in ASP.NET MVC

The UrlHelper.GenerateUrl in ASP.NET MVC can be used to return a string which contains the URL. UrlHelper.GenerateUrl in ASP.NET MVC The UrlHelper.GenerateUrl takes 7 parameters (overloaded). These parameters include RouteName ActionName ControllerName Route Values Route Collection Request Context Include Implicit Mvc Values Below is a sample code snippet demonstrating the UrlHelper.GenerateUrl

How to Generate Links to Action Method in different Areas in ASP.NET MVC?

The ActionLink helper method has 9+ overloads to generate links based on various scenarios. Generally, the “Action Name” is passed as the 2nd parameter to the ActionLink which points to the current Controller and the specified Action method. What do we do when the Action method of a different Area needs to be refereed to when generating the link in ASP.NET MVC? How to Generate…

5 Alternative ASP.NET MVC View Engines

If you a ASP.NET MVC Web developer , you would have noticed that by default ASP.NET MVC includes 2 View Engines “Razor View” and “ASPX View”. In this article , we will explore other different View Engines(third party) available in ASP.NET. 5 Alternative ASP.NET MVC View Engines 1. Spark Spark is a open source view engine for ASP.NET MVC projects as well as Castle MonoRail…

Creating the first ASP.NET MVC Project

In this article , we will explore how to create the first “Hello World” ASP.NET MVC Project. How to Create an ASP.NET MVC Project ? Follow the below steps to create the first ASP.NET MVC Project. 1. Launch Visual Studio 2012 . 2. Click New -> Project 3. This will show the list of all available templates available currently in Visual Studio 2012. Select Web…

How to retrieve all the Errors from the ModelState in ASP.NET MVC ?

To display all the possible error messages from the ModelState in ASP.NET MVC , the developers would use the Html.ValidationSummary helper method in the View . How to get the list of all the Errors from the ModelState in ASP.NET MVC Controller ? The ModelState.Values property is a ICollection which contains the list of all the states which the developers can use to filter out…

Url.Action in ASP.NET MVC

The Url.Action method in ASP.NET MVC allows the developers to generate the fully qualified URL to an action method. It is defined in the namespace System.Web.Mvc. The Url.Action generates only the URL unlike the Html.ActionLink. Url.Action in ASP.NET MVC Below is a code snippet of the View demonstrating the Url.Action

How to accept HTML Form Data in a ASP.NET Web API Action Method ?

There are times when you might want to create an endpoint in ASP.NET WebAPI that can handle the HTML form data posted as x-www-form-urlencoded. One of the options is to use the model binding concept.   In this case , the model should have the names of the keys that are passed in the HTTP request. The MediaFormatters are used to extract the data from…