Tuesday, 24 July 2018

MVC Error Handling


4 Ways to do MVC Error Handling

Between .NET, ASP.NET, and MVC there are several potential ways to handle application errors.

  • Web.Config customErrors
  • MVC HandleErrorAttribute
  • Controller.OnException method
  • HttpApplication Application_Error event

Must Have: Global Error Page With Web.Config <customErrors>
The last thing you ever want your users to see is a “yellow screen of death” type error. If you don’t know what that is, I’m referring the standard yellow ASP.NET error screen.
For any application, I would always recommend specifying a custom error page in your Web.Config. Worst case scenario, your users will see this page if an unhandled exception occurs.
<system.web>
    <customErrors mode="On" defaultRedirect="~/ErrorHandler/Index">
        <error statusCode="404" redirect="~/ErrorHandler/NotFound"/>
    </customErrors>
<system.web/>

Use MVC HandlerErrorAttribute to Customize Responses
The HandleErrorAttribute inherits from FilterAttribute and can be applied to an entire controller or individual controller action methods.
It can only handle 500 level errors that happen within an MVC action method. It does not track exceptions that help outside of the MVC pipeline. Exceptions may occur in other HTTP modules, MVC routing, etc.
When to Use HandleErrorAttribute
Since it does not provide a way to collect all exceptions that could ever happen, it is a bad solution for a global unhandled error handler.
It works perfectly for tailoring specific error pages for a specific MVC controller or action method. Specifying an error page in your Web.Config <customErrors> works ideal for a universal error page. The HandleErrorAttribute gives you fine-grained control if you need it.

Note: HandleErrorAttribute requires customErrors to be enabled in your Web.Config.
For example, if you wanted to show a particular MVC view when a SqlException happens, you can do it with the code below:
[HandleError(ExceptionType = typeof(SqlException), View = "SqlExceptionView")]
public string GetClientInfo(string username)
{            
   return "true";
}

The problem with HandleErrorAttribute is it doesn’t provide a way to log the exception!

Use MVC Controller OnException to Customize Responses
OnException is similar to HandleErrorAttribute but provides more flexibility. It works with all HTTP status codes, and not just 500 level responses. It also gives you the ability to log the errors!
public class UserMvcController : Controller
{
   protected override void OnException(ExceptionContext filterContext)
   {
      filterContext.ExceptionHandled = true;
                 //Log the error!!
      _Logger.Error(filterContext.Exception);
      //Redirect or return a view, but not both.
      filterContext.Result = RedirectToAction("Index", "ErrorHandler");
      // OR 
      filterContext.Result = new ViewResult
      {
         ViewName = "~/Views/ErrorHandler/Index.cshtml"
      };
   }
}

When to Use OnException for MVC Error Handling
If you want a way to present your users custom MVC views or custom log exceptions, OnException is a good solution for you. It provides more flexibility than HandleErrorAttribute and does not require customErrors to be enabled in your Web.Config file.
Note: OnException gets called for all HTTP status codes. So be careful how you handle simple issues like a 404 caused by a bad URL.
public class BaseController : Controller

    {

        protected override void OnException(ExceptionContext filterContext)

        {

            filterContext.ExceptionHandled = true;

            ViewData["Data"] = filterContext.Exception.Message.ToString();

            filterContext.Result = new ViewResult { ViewData = this.ViewData, ViewName = "Error" };

            base.OnException(filterContext);

        }

    }
Use HttpApplication Application_Error as Global Exception Handler
So far we have covered three different ways to customize the response that your users see if an exception occurs. Only within OnException can you potentially log exceptions.
To log all unhandled exceptions that may occur within your application, you should implement basic error logging code as shown below.
public class MvcApplication : System.Web.HttpApplication
{
   protected void Application_Start()
   {
      AreaRegistration.RegisterAllAreas();
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
      RouteConfig.RegisterRoutes(RouteTable.Routes);
      BundleConfig.RegisterBundles(BundleTable.Bundles);
   }
   protected void Application_Error()
   {
      var ex = Server.GetLastError();
      //log the error!
      _Logger.Error(ex);
   }
}
When to Use Application_Error
Always! HttpApplication’s Error even provides the best mechanism to collect and log all unhandled application errors.




Thursday, 12 July 2018

Exporting & Importing Websites and AppPoll configuration between multiple IIS instances


Export the all Application Pools
The first thing you have to do is to export/import the application pools, since they will most likely be used in your web sites and you won’t be able to import those without having their relevant app pool into place. Here’s the command-line: 
%windir%\system32\inetsrv\appcmd list apppool /config /xml > c:\apppools.xml
This command will export all your application pools – including the default ones. You’ll need to remove those, as they will most likely be in your target IIS instance with the same name and therefore they will raise a duplicate name error, blocking the whole import. In order to avoid that, open the newly created apppools.xml  file and remove the default ones such as:
·        DefaultAppPool

·        Classic .NET AppPool

·        .NET v2.0

·        .NET v2.0 Classic

·        .NET v4.5

·        .NET v4.5 Classic

Import the Application Pools
Copy the apppools.xml  file to your target webserver and run the following command:
1
%windir%\system32\inetsrv\appcmd add apppool /in < c:\apppools.xml
Each and every Application Pool mentioned in the xml file will be created on your target IIS instance.

Export the all Websites
Open up again a command-line console on your source webserver and type in the following command:
%windir%\system32\inetsrv\appcmd list site /config /xml > c:\websites.xml
Again, you’ll have to remove the default websites –  you’ll most likely have only one default website, which is Default Website – as well as any other website you don’t want to copy and/or is already existing on the target IIS instance, otherwise the import command won’t work.

Import the Websites
Just like you did with the App Pools file, copy the websites.xml  file to your target webserver and run the following command: 
%windir%\system32\inetsrv\appcmd add site /in < c:\websites.xml

Export/Import a single App Pool or Website
These commands can also be used to export/import a single application pool or a specific website. You just have to add their identifying name to the command-line, such as:
Export a specific Application Pool
%windir%\system32\inetsrv\appcmd list apppool “CustomAppPool” /config /xml > c:\customapppool.xml
Import a specific Application Pool
%windir%\system32\inetsrv\appcmd add apppool /in < c:\customapppool.xml

Export a specific Website
%windir%\system32\inetsrv\appcmd list site “CustomWebsite” /config /xml > c:\customwebsite.xml
Import a specific Website
%windir%\system32\inetsrv\appcmd add site /in < c:\customwebsite.xml

2 Using Export configuration in IIS. 
I'd say export your server config in IIS manager:
In IIS manager, click the Server node
Go to Shared Configuration under "Management"
Click “Export Configuration”. (You can use a password if you are sending them across the internet, if you are just gonna move them via a USB key then don't sweat it.)
Move these files to your new server
administration.config
applicationHost.config
configEncKey.key 
On the new server, go back to the “Shared Configuration” section and check “Enable shared configuration.” Enter the location in physical path to these files and apply them.
 It should prompt for the encryption password (if you set it) and reset IIS.

http error 403.14 – forbidden: the web server is configured to not list the contents of this directory. Mvc


When we publish MVC application to IIS sometimes we got this error.
Following are the possible causes/ fixes for this 
·        Make sure the Application pool targets correct version of .NET framework (i.e .NET Framework v4.0.30319 for .NET 4 and 4.5)

·        Make sure the Pipeline mode of IIS Application pool is "Integrated"

·        Check UrlRoutingModule-4.0 is added in the modules of that website, if not add following into Web.config module section.
<add name="UrlRoutingModule-4.0"
           type="System.Web.Routing.UrlRoutingModule"
            preCondition="" />
<remove name="FormsAuthenticationModule" />

·        Make sure you have following element added in system.webServer section of website's web.config

<system.webServer>
<modules runAllManagedModulesForAllRequests="true"></modules> 
</system.webServer>