Handling custom error in mvc

In asp.net mvc there is an custom error in web.config like below:

  1. <customErrors mode="On">
  2.  <error statusCode="404" redirect="~/404.html"/>
  3.  </customErrors>

Sometimes you want to redirect the user to your own custom error controller and log the error message. You can handle it! Firstly create your own Error controller:

  1.  public class ErrorController : Controller
  2.     {
  3.         public A... Continue

Multiple submit buttons in asp.net MVC

In asp.net MVC you can submit a form by @using(Html.BeginForm()). You can Post the model you have binded via this operation and command the begin form to post to a Post action:   @using(Html.BeginForm("Create","FooController")).

Now if you have more than one submit button to send the page content to different post action, there are few ways: one of them is to post the content to a specific action and then decide in it to redirect which action:


  1. <input type="submit" value="Field1" name=&quo... Continue

How to turn off the caches in asp.net MVC

I've written about different kinds of caching in asp.net MVC here, I just wanted to turn off some specific caches in my project and found a way to do that, you can do that by overriding OnResultExecuting:

  1. public class TurnOffCache : ActionFilterAttribute
  2. {
  3.     public override void OnResultExecuting(ResultExecutingContext filterContext)
  4.     {
  5.         filterContext.HttpContext.Response.Cache.SetE... Continue

Attribute routing with asp.net MVC5

Until the latest version of asp.net (asp.net mvc4), routing was handling by Convention-based routing to matches a URI to an Action in a separate class (RouteConfig) registered in global.asax. but in asp.net MVC5  you can use attribute routing. It means you can use attribute to set routes to your actions. It's too easy getting started with attribute routing, you just need to refer RouteConfing.cs in App_Start folder and add  routes.MapMvcAttributeRoutes(); in RegisterRoutes:

  1. &... Continue

How to create portable area in asp.net mvc4

Sometimes you need to separate your areas from MVC project into another class library for making it readable and better management.  you can do that by using features of MvcContribe project. I recommend you to create your area in asp.net MVC and then transfer the content to the class library:




Now add a class library to your solution and transfer the content(Controllers, ... Continue

How to use PagedList In asp.net MVC

PagedList.Mvc is one of many good paging and sorting packages for ASP.NET MVC, in the below simple example I'm gonna to implement it for content (posts) of a blog post, install the PagedList.Mvc via nuget and take a look at this code. This is an action which returns blog posts:

  1.        public ActionResult List(int? page)
  2.         {
  3.             var posts = _blogService.GetAllBlogPosts();
  4.             var pageNumber ... Continue

How to create UrlSlug in Asp.Net MVC

UrlSlug Is a way of generating a valid Url, and using the title of an article to generate a URL. UrlSlug is very important in CEO because google likes to index the meaningful Urls at the first and then it refers to other Urls. Spouse you wanna to create the this Url:

  1.  MyWebSite.com/Blog/Post/2013/4/14/how-to-create-url-slug-in-aspnet-mvc

create the model class :

  1.     public class Blog
  2.     {
  3.         public int Id { get; set; }
  4.         public s... Continue

ActionResult types in Asp.net MVC

Controller actions returns the ActionResult and it's possible to return kinds of results based on what we want in the output , let’s consider for types of result in by example in this article.

ActionResult: it just Initializes a new instance of the ActionResult class. ActionResult is the base class for other types of action.

  1.         //Action Result
  2.         public ActionResult Index()
  3.         {
  4. &nbs... Continue

Fluent Nhibernate Sample Project in Asp.net MVC

Nhibernate is an ORM designed for Microsoft.Net, it's free and open source and it's a port of java mapper to Dot Net. And fluent Nhibernate is a separate version of Nhibernate which lets you write your entities according to your business instead of starting from creating tables, fluent Nhibernate is just a fluent API for mapping classes with NHibernate. what an ORM does, is persisting object In database and then retrieving when it is needed, indeed it translates the object to database and vice versa.

In Entity Framework yo... Continue

Using FluentValidation in MVC

Based on CodePlex definition: "FluentValidation is A small validation library for .NET that uses a fluent interface and lambda expressions for building validation rules for your business objects."

There is no any problem to use Asp.net MVC built in DataAnnotation but in some cases it's better to use FluentValidation instead. I list my reasons about which should be selected to use here.

Use DataAnnotation if :

Your system is simple and you don't have any complex valid... Continue