How to do it...

  1. Your current ASP.NET Core application should contain a Configure() method that looks as follows:
        public void Configure(IApplicationBuilder app, 
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.Run(async (context) =>
{
await context.Response.WriteAsync($"The date is
{DateTime.Now.ToString("dd MMM yyyy")}");
});
}
  1. From the Debug menu, click on Start Without Debugging or press Ctrl + F5. You will see the date displayed as follows:
  1. Go back to your code and tell your application to display the welcome page middleware. You can do this by adding app.UseWelcomePage(); just before the app.Run(). Your code needs to look as follows:
        if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseWelcomePage();

app.Run(async (context) =>
{
await context.Response.WriteAsync($"The date is
{DateTime.Now.ToString("dd MMM yyyy")}");
});
  1. Save your Startup.cs file and refresh your browser.
  1. You now no longer see the date displayed on the screen. This is because the welcome page is the terminating middleware and any HTTP requests do not pass through that. Go ahead and modify the welcome page middleware to look as follows:
        app.UseWelcomePage("/hello");
  1. If you save your file and refresh your browser now, you will see the date displayed in the browser again. So what happened? Well you just told the welcome page middleware to only respond to requests for a /hello page.
  2. Change the URL in the browser as follows http://localhost:25860/hello and press Enter. The welcome page is displayed again.
  3. Let's take a look at the UseDeveloperExceptionPage() middleware. Modify app.Run() to look as follows:
        app.Run(async (context) =>
{
throw new Exception("Error in app.Run()");
await context.Response.WriteAsync($"The date is
{DateTime.Now.ToString("dd MMM yyyy")}");
});
  1. Save your changes and refresh your browser. You will see that the browser now displays a page that a developer will find extremely useful. It displays the stack information, the incoming query, any cookies as well as the header info. It even tells us on what line the exception happened (line 36 in the Startup.cs file). The UseDeveloperExceptionPage() middleware allows the request to pass through it to the lower middleware. If an exception happens, this would then allow the UseDeveloperExceptionPage() middleware to do its job. As mentioned earlier, the placement of middleware is important. If we had this UseDeveloperExceptionPage() middleware at the end of the page, it wouldn’t catch any unhandled exceptions. It is, therefore, a good idea to have this at the top of your Configure() method:
  1. Let's take this concept further. When we are in a production environment, we would typically not want the user to see the exception page. Assume that they need to be directed to a friendly error page. Start off by adding a static HTML page to the wwwroot of your application. Right-click on the wwwroot and select Add, New Item from the context menu:
The wwwroot is where you would serve static pages such as JavaScript files, CSS files, images, or static HTML pages.
  1. Select an HTML page, call it friendlyError.html and click on Add.
  1. Modify the HTML of friendlyError.html as follows:
        <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Friendly Error</title>
</head>
<body>
Something went wrong. Support has been notified.
</body>
</html>
  1. What we need to do next is add a NuGet package to our application so that we can serve static files. In the NuGet Package Manager, search for Microsoft.AspNetCore.StaticFiles and add that to the application.
  2. Now, we need to modify the code slightly to simulate that it is running in a production environment. We do this by setting theEnvironmaneName property of the IHostingEnvironment interface as follows: env.EnvironmentName = EnvironmentName.Production;.
  1. We then need to add an else statement to the if (env.IsDevelopment()) condition and write the code to call our custom, static error page. It is here that we will add the friendlyError.html file to our DefaultFileNames() collection and tell our application that we want to use this error file on any exceptions in the production environment. Lastly, we need to tell our application to use static files by calling the UseStaticFiles() method. When complete, your code should look as follows:
        env.EnvironmentName = EnvironmentName.Production;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Add("friendlyError.html");
app.UseDefaultFiles(options);

app.UseExceptionHandler("/friendlyError");
}

app.UseStaticFiles();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.83.176