Tuesday, 28 July 2015

ChildACtionOnly Attribute in Mvc

Any action method that is decorated with ChildActionOnly attribute is a child action method
Child action methods  will not respond to URL requests. If an attempt is made, a runtime error will be thrown stating –child action is accessible only by   a child request.
Child action methods can be invoked by making child request from a view using “Action()” and “RenderAction() ” html helpers.
An action method doesn’t need o have childactiononly attribute to be used as a child action, but use this attribute to be used as a child action, but use this attribute to prevent a action method from being invoked as a result of user request.
Child actions are typically associated with partial views, but this is not compulsory.
Child action methods are different from Non Action methods, in that non action methods cannot be invoked using Action() or RenderAction() helpers.

Here i will explain about ChildActionOnly Attribute
Create one sample controller in your mvc application,write two action methods name it as index and childaction methods and add respective views .

 public ActionResult Index()

        {

            ViewBag.TempValue = "Index action called at Sample controller";

            return View();

        }


        [ChildActionOnly]

        public ActionResult ChildAction(string param)

        {

            ViewBag.Message = "Child Action Called"+param;

            return View();

        }


suppose if we call Childaction method through url you will get an error.
Child action methods  will not respond to URL requests. If an attempt is made, a runtime error will be thrown stating –child action is accessible only by   a child request.







Now we need to call the childaction method from index view

@{

    ViewBag.Title = "Index";

}

<h2>Index</h2>

<h3>@ViewBag.TempValue  </h3>  

@Html.Action("ChildAction", "Sample", new {param=" first time" })



Here we are passing one parameter ,so that we will get this in index view.

let's see childaction view 


@{

    ViewBag.Title = "ChildAction";

}

<h2>ChildAction</h2>

<h3>@ViewBag.Message</h3>

now call the index action method in sample controller 

the child action view will see with in the index view

Using child action methods, it is possible to cache portions of a view. This is main advantage of child action methods

tags:child action attribute in mvc 5,child action attribute in mvc 6,child action attribute in mvc 4,action filters in mvc,
filters mvc5,filters in mvc 4,filters in mvc6,child action filter in mvc,child action method,action method in mvc 5,action methods in mvc 5,action methods in mvc 6

Monday, 27 July 2015

Authorize and AllowAnonymous Action Filters

Authorize Attribute:

In Asp.net Mvc all the controller action methods are accessible to both anonymous and authenticated users. If you want action methods to be available only authenticated and authorized users, then use authorize attribute.

AllowAnonymous Attribute:

Allowanonymous attribute used to skip authorization enforced by Authorize attribute.

tags:authorize attribute in mvc 5,authorize attribute in mvc6,authorize attribute in mvc 4,allow anonymous attribute in mvc4,anonymous attribute in mvc5,anonymous attribute in mvc6,action filters in mvc 5,action filters in mvc 4,action filters in mvc 6,filters in web api,filters in mvc 5,authorize attribute in asp.net mvc 4 5 6,allowanonymous attribute in asp,net mvc 5,allowanonymous attribute in asp,net mvc 6,allowanonymous attribute in asp,net mvc 4,