Action Filters with ASP.Net MVC

Hi Friends,

In this section am going to discuss brief about Action Filters and how this is applied in the ASP.Net MVC application. But, before jumping into the details of Action Filters, let’s look @ the definition of Action Filters:- “Action filters are the components that you want to apply to logics which are going to be used across multiple controllers. So, the significance of this, it actually prevents duplication of code inside each controller. ”

There, could be as many

Name Description
OutPut Cache Cache the Output of a controller
Authorize Technique to force only authentic user to login
ValidateAntiForgeryToken Prevents cross site forgeries
HandleError In case of unhandled exception, view can be specified
Validate Input Dangerous one, turn off request validation and allow any input

Now, in case of OutPut cache attribute,

This tells the run time that it’s allowed to cache the final output of some action and use it again till the specified time. when you apply these attributes to few places, you can dramatically increase the performance of the application. Another example could be authorize attribute, it’s job is to check whether authentic user is logged or not, this is many times tied up with roles as well.

Now, let’s look at the small example below for Authorize attribute

[Authorize]
[HttpGet]
public ActionResult Search(string name=”Titanic”)
{
var message = Server.HtmlEncode(name);
return Content(message);
}

Now, when i build and invoke the below url, it will

/Movie/Titanic

it will produce the login screen as shown below:-action filters

so, this is the action filter in brief. For the deep dive and know more about the Actions Filters in action subscribe to ONLINE ASP.Net MVC workshop. Till then stay tuned and happy coding.

Thanks,
Rahul

1,289 total views, no views today

Use of Http Accept Verbs in MVC

Hi Friends,

Let me give one scenario, where in it becomes very necessary to use HTTP Accept verbs to identify the action whether it’s a Get Request or Post or Delete or Put or combination of any these.

Let’s look @ the below example

public ActionResult Search(string name=”Titanic”)
{
var message = Server.HtmlEncode(name);
return Content(message);
}

public ActionResult Search()
{

return Content(“Hello world”);
}

when you build the solution, it will build successfully, but when you invoke the app, it will give you below error as shown below.

MVC_accept_verb

Here, what happened exactly, MVC framework tries to invoke the search action but unable to distinguish between the same, hence produced the exception. so, for these kind of scenarios, we have HTTP Accept verbs which literally tells the C# compiler to treat one response for HTTP Get request and the other one for HTTP Post.

[HttpGet]
public ActionResult Search(string name=”Titanic”)
{
var message = Server.HtmlEncode(name);
return Content(message);
}

[HttpPost]
public ActionResult Search()
{

return Content(“Hello world”);
}

Now, when i do the build and refresh the browser, it will produce the desired result. Actual scenario, will be explained in a much cleaner way in the upcoming workshop for ASP.Net MVC. Till then stay tuned and happy coding.

Thanks,
Rahul

2,258 total views, no views today