Login for your services
Digital Diary My Tasks Bookmarks My Q/As
AXARROW HIGHLIGHTS
This article describes how to avoid exposing Partial Views as direct calls from Browser and restrict them within the main Views only. Handling exception as InvalidOperaionException is also discussed.
Recent Articles
VIEW ARTICLE

ASP.NET MVC Partial Views Error Handling And Management

Author: Suresh Kr Chaudhary
Category: Programming
Submitted On: 7/28/2017
Published On:
ABSTRACT: This article describes how to avoid exposing Partial Views as direct calls from Browser and restrict them within the main Views only. Handling exception as InvalidOperaionException is also discussed.

ASP.NET MVC Partial Views Error Handling And Management

In any application based on ASP.NET MVC model, Views represent application interface to the user. A typical application consists of mutiple modules and each module may contain multiple Views. So in an application of reasonably good size, there exist dozens of Views and in Enterprise level application, number of such Views run into hundereds.

Partial Views

A View generally covers the entire user screen. In certain scenarios, a User screen or View may contain multiple sub-screens generally presented to the user by sub-Views. These sub-Views may technically be called as Partial Views. They present multiple advantages to the user, for instance to update smaller piece of information related to the big piece being shown on rest of the screen. Such smaller portion of information is presented to user by the partial View. There is not need for the entire screen to get updated every time the server is being requested for information. This creates a good user experience, apart from making best utilization of the server resources.


Controllers are basically the components that control the Views and their behaviour. They call-in Model, View-Models and apply required application-logic and build up the desired scenario for the use-case in question.

In this article, we are covering few scenarios faced by ASP.NET MVC developers, with regard to rendering of partial-Views. There are situations where partial Views are to be handled differently than the main Views. In the Visual Studio project. the very first thing that we do with partial Views, is to name the partial View by having an under score character (_) in the beginning of name, so that they could be distinguished properly.

Secondly the Controller method uses the "return PartialView()" method instead of "return View()" method.


     public ActionResult FormSections(bool? shortparam)
        {
            ViewBag.shortParam = shortParam; // data being passed to partial View, if required so... 
            return PartialView("_FormSections", db.FormSections.Where(s => s.Code != "ABC").ToList());
        }


The above code represents a Controller method named "FormSection()" that sends the user a partial View with name "_FormSections". The partial View code is assumed to be present in the file _FormSections.cshtml in the Views folder.

Invoking partial View
Partial Views are invoked via controller methods either as a pure Child action method, or independent call via Javascript /JQuery Ajax method call. The developer needs to decide whether the call is a pure child-action or via Ajax method, and handle the scenario accordingly.

If it is a pure Child action method call, the method cannot be invoked directly through the browser, and if it is done so, the program throws an exception InvalidOperationException. In this scenario, the method is decorated with a special tag as below.


     [ChildActionOnly]
     public ActionResult FormSections(bool? shortparam)
        {
            ViewBag.shortParam = shortParam; // data being passed to partial View, if required so... 
            return PartialView("_FormSections", db.FormSections.Where(s => s.Code != "ABC").ToList());
        }


The method could be invoked from within the main View page code like
{ Html.RenderAction("FormSections", "MySections", new { shortParam = true }); }

Since the program throws an exception, this exception can be handled gracefully by placing proper Exception handling method(s) in the "Global.asax.cs" file.
Upon catching the Exception, the developer may want to redirect the user to some appropriate Application Error-handling page or to the root page of the application.

If the partial View method is to be called /invoked via an Ajax Call from within the Javascript/JQuery embedded within the main View page code, the method can have a check for the same so that it could be invoked via Ajax call only.


// [ChildActionOnly]  // Need to remove this tag
public ActionResult Create(int? SomeID)
{
     // Check it explicitely
     if(!HttpContext.Request.IsAjaxRequest()) 
            return RedirectToAction("Index", "Home"); // as per your choice...
            
                ...
                ...
            your code here...
    return PartialView("_Create",.....);
    }
     


The above code handles the exception and redirects the user to some page.

It is also important to note that you also need to restrict Search Engine Crawlers to index your page that you do not want to be exposed directly to the browser call.


Go to Top
ASP.NET MVC Partial Views Error Handling And Management

Related Questions...(0) Ask A Question

Ask a Question

Go to Top