ASP.NET Core: Dependency Injection and Services in MVC

http://www.mikesdotnetting.com/article/285/asp-net-core-dependency-injection-and-services-in-mvc

 

Advertisements

Delegates and Lambda Expressions

public static void ExecuteCSharp3_0()
	{
	    // declare the lambda expression
	    IncreaseByANumber increase = j => j * 42;
	    // invoke the method and print 420 to the console
	    Console.WriteLine(increase(10));

	    MultipleIncreaseByANumber multiple = (j, k, l) => ((j * 42) / k) % l;
	    Console.WriteLine(multiple(10, 11, 12));
	}

Type inference helps the compiler to infer the type of j from the declaration of the IncreaseByANumber delegate type. If there were multiple arguments, then the lambda expression could look like this:

	MultipleIncreaseByANumber multiple = (j, k, l) => ((j * 42) / k) % l;
	Console.WriteLine(multiple(10, 11, 12));


http://stackoverflow.com/questions/667742/callbacks-in-c-sharp


ASP.NET MVC 3: Layouts with Razor

http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts

_ViewStart.cshtml :

The _ViewStart file can be used to define common view code that you want to execute at the start of each View’s rendering.  For example, we could write code like below within our _ViewStart.cshtml file to programmatically set the Layout property for each View to be the SiteLayout.cshtml file by default:

Question:

If I want to trigger something right before the page is output, should I put the code in _viewstart.cshtml of others?

Answer:

The _ViewStart file can be used to define common view code that you want to execute at the start of each View’s rendering. For example, we could write code within our _ViewStart.cshtml file to programmatically set the Layout property for each View to be the SiteLayout.cshtml file by default

Actually it’s like a BasePage in ASP.Net where we can keep the common code.

Or you can write the logic directly in the View like below.

@{
  Layout = "~/Views/Shared/_Layout.cshtml";
  if (Some Consition) {
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
  }
}

Alternatively

You can override the Action Executing method, which executes before executing the Action Method. You can set it for a particular Action method or for the Complete Controller

Below is the code for setting it for Complete Controller.

protected override void OnActionExecuting(ActionExecutingContext ctx) {
    base.OnActionExecuting(ctx);
}

Below is the Code for Setting it for Particular Action method

[MyAttribute(SomeProperty = "")]
public ActionResult Index()
{
     return View("Index");
}

public class MyAttribute : ActionFilterAttribute
{
    public string SomeProperty { get; set; }
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
    }
}