TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.Net Action Filters

ASP.Net Action Filters with asp.net tutorial, asp.net introduction, features, project, example, server controls, labels, textbox, button, hyperlink, radiobutton, calender, checkbox, fileupload, events handling, authentication, webforms model binding, html server control, compare validdator, range validator, validation summary, mvc introduction, mvc project, view, validation, entity framework, authentication etc.

<< Back to ASP

ASP.NET MVC Action Filters

The MVC framework provides the filter attribute so that we can filter the user requests. We can apply it to an individual action or an entire controller. It completely modified the way in which an action is called.

The ASP.NET MVC Framework provides the following action filters.

OutputCache: it makes controller's action output cacheable for the specified time.

HandleError: It is used to handle error raised when a controller action executes.

Authorize: It allows only authorize user to access resources.


OutputCache Example

Here, we are implementing OutputCache that will cache the action method output for the specified time. The action code is given below:

// MusicStoreController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplicationDemo.Controllers
{
    public class MusicStoreController : Controller
    {
        [OutputCache(Duration =10)]
        public ActionResult Index()
        {
            return View();
        }
    }
}

It renders an index file that has the following code.

// index.cshtml

<h4>
    @{ 
        Response.Write( DateTime.Now.ToString("T"));
     }
</h4>

Output:

It produces the following output and cache that for 10 seconds. It will not change before 10 seconds even we refresh the web page again and again.

ASP Action filters 1

Authorize Example

Now, we are applying authorize attribute to the action method, the code is given below.

// MusicStoreController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplicationDemo.Controllers
{
    public class MusicStoreController : Controller
    {
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }
    }
}

Output:

This attribute will restrict unauthorized user access. The application redirects to the login page for authentication.

ASP Action filters 2
Next TopicASP.NET MVC Model




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf