Reputation: 19
I am a fresh intern who has just completed my first year of college. I took two intro to comp sci classes and have a fairly solid fundamental knowledge of Java but also basic OOP language ideas. I have never used C# before and my internship requires it. My task has been to start a MVC application that does some basic task but I am confused on the usage of 'context' in this situation. I am using Microsoft Virtual Studio with the ASP.NET MVC blank template. Here is where I find 'context' to be used. I am mainly confused on how or why it is being used in this method.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Use
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
Upvotes: 1
Views: 784
Reputation: 6060
This code
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
Tells ASP.NET Core to "Run" a middleware. Running a middleware means, given an HttpContext
, read the request and write the result-- as opposed to "using" a middleware which means the middleware might execute or might defer execution to the next middeware in the chain. This is specific to ASP.NET Core, not C#.
The app.Run()
method expects a delegate in the form of an async function that takes a single HttpContext
parameter. The lamda expression (context)=> { }
is shorthand for an anonymous function with a single parameter named context
. The C# compiler recognizes the type of context
based on the expected prototype. You might write that code like this:
app.Run(helloWorldHandler);
...
private async Task helloWorldHandler(HttpContext context) {
await context.Response.WriteAsync("Hello World!");
}
Upvotes: 1