Explain InProcess and OutOfProcess ASP.NET Core Hosting?

In general, there are two modes in which an ASP.NET Core application can run, when deployed in IIS.

In-Process model:

  • The application is hosted inside the IIS process and all the functioning happens from here. This is similar to any other general website deployed in IIS (through binaries).

Out-Of-Process model:

  • The application resides within the Kestrel Shell layer, and the IIS server acts as a reverse-proxy - all the requests travel from within the IIS Web server which is available to the outside internet, and the proccessing happens within the Kestrel.

How do you specify?

  • To explicitly specify the mode we want our application to run, we provide it inside our Web project CSPROJ file. It can look something like this
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
    <AspNetCoreModuleName>AspNetCoreModule</AspNetCoreModuleName>
  </PropertyGroup>

  .... other config ....
</Project>
  • If we don't specify the type of hosting model we're into, the ASP.NET Core bundle hosts the application in an In-Process manner.

How do you create an Authorization Filter?

Authorization Filters act at the beginning of the request life cycle, before all other filters are executed. It determines if the request is authorized to access the resource or not.

To create an Authorization Filter, one can simply implement the IAuthorizationFilter interface and provide an implementation of the method:

interface IAuthorizationFilter {
  OnAuthorization(AuthorizationFilterContext ctx);
}

How do you create an Action Filter?

An ActionFilter can be created in two ways:

  1. Implement the IActionFilter interface
  2. Extend the ActionFilterAttribute abstract class

IActionFilter contains two methods, that the custom Action Filter needs to implement:

OnActionExecuting(ActionExecutingContext ctx) - before action is executed
OnActionExecuted(ActionExecutedContext ctx) - after action is executed

ActionFilterAttribute abtract class has the below methods which can be overriden as required:

OnActionExecuting(ActionExecutingContext ctx) - before action is executed
OnActionExecuted(ActionExecutedContext ctx) - after action is executed
OnResultExecuting(ResultExecutingContext ctx) - before the ActionResult instance is invoked
OnResultExecuted(ResultExecutedContext ctx) - after the ActionResult instance is invoked

Can ASP.NET Core WebAPI Actions have void return types?

ASP.NET Core WebAPI actions CAN have void return types. The framework returns an EmptyResult for such actions with a default 200 (OK) status Code.

This can be particularly observed in the cases of HttpDelete operations where no return is expected by the client in most cases.

Explain Map, Reduce and Filter functions.

Map, Reduce and Filter are three functions which operate on a given array of values.

  • Map function works like a projection, where we can project each value from the input set into some transformed output.
  • It takes a callback function which has two parameters - the current value and the index of the current value and expects a return value from the callback.
let arr = [1, 2, 3, 4, 5];
let result2 = arr.map(function (current, index) {
    return current % 2;
});
// output:
[ 1, 0, 1, 0, 1 ]
  • Reduce function works like an aggregate function, where we can create an aggregation from a given input set.
  • It takes a callback function which has three parameters - the previous value, the current value and the current index and expects a return value from the callback, which is applied onto the next array element iteration.
let result1 = arr.reduce(function (prev, current) {
    console.log(`${prev}, ${current}`);
    return prev + current;
});
// output:
1, 2
3, 3
6, 4
10, 5
result1: 15
  • Filter function is an extraction function where the input array is filtered based on a condition.
  • It takes a callback function which has two parameters - the current value and the current index and expects a boolean return value which represents whether the current element satisfies the condition or not.
let result3 = arr.filter(function (current, index) {
    return current % 2 === 0;
});
// output:
[ 2, 4 ]

How can you extract the id attribute of all the elements in the document with JavaScript?

We can do it in three steps.

  1. Pull all the elements in the document irrespective of their Tag.
  2. Loop through all the Elements and see if the element has an attribute id
  3. If exists collect the attribute value into the output array
<!DOCTYPE html>
<html>
<body id="myBody">
    <div id="mainContainer" class="container-fluid">
        <div id="firstRow" class="row">
            <div id="fullCol" class="col-md-12">
                <h1 id="headingText">Hello World!</h1>
                <p id="subHeadingText">
                This is some sample text you're viewing on the Page.</p>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        (function () {
            let elements = document.getElementsByTagName("*");
            let ids = [];
            for (let i = 0; i < elements.length; i++) {
                let element = elements[i];
                if (element.id) {
                    ids.push(element.id);
                }
            }
            console.log(JSON.stringify(ids));
        })();
    </script>
</body>
</html>

output:
["myBody","mainContainer","firstRow","fullCol","headingText","subHeadingText"]