ارتقاء به ASP.NET Core 1.0 - قسمت 5 - فعال سازی صفحات مخصوص توسعه دهندهها
نویسنده: وحید نصیری
تاریخ: ۱۳۹۵/۰۴/۱۹ ۱۲:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public void Configure(IApplicationBuilder app)
{
app.Run(async context =>
{
throw new Exception("Generic Error");
await context.Response.WriteAsync("Hello DNT!");
});
}
public void Configure(IApplicationBuilder app)
{
app.UseDeveloperExceptionPage();
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
if(env.IsDevelopment()){ }
if(env.IsProduction()){ }
if(env.IsStaging()){ } public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(errorHandlingPath: "/MyControllerName/SomeActionMethodName");
} var feature = HttpContext.Features.Get<IExceptionHandlerFeature>(); var error = feature?.Error;
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseStatusCodePages();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(errorHandlingPath: "/MyControllerName/SomeActionMethodName");
}
}
app.UseStatusCodePagesWithReExecute("/MyControllerName/SomeActionMethodName/{0}"); <customErrors mode="On" defaultRedirect="error">
<error statusCode="404" redirect="error/notfound" />
<error statusCode="403" redirect="error/forbidden" />
</customErrors> public void Configure(IApplicationBuilder app)
{
if (env.IsDevelopment())
{
app.UseDatabaseErrorPage();
app.UseDeveloperExceptionPage();
}
app.UseExceptionHandler("/error/index/500");
app.UseStatusCodePagesWithReExecute("/error/index/{0}"); public class ErrorController : Controller
{
private readonly ILogger<ErrorController> _logger;
public ErrorController(ILogger<ErrorController> logger)
{
_logger = logger;
}
public IActionResult Index(int? id)
{
var logBuilder = new StringBuilder();
var statusCodeReExecuteFeature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
logBuilder.AppendLine($"Error {id} for {Request.Method} {statusCodeReExecuteFeature?.OriginalPath ?? Request.Path.Value}{Request.QueryString.Value}\n");
var exceptionHandlerFeature = this.HttpContext.Features.Get<IExceptionHandlerFeature>();
if (exceptionHandlerFeature?.Error != null)
{
var exception = exceptionHandlerFeature.Error;
logBuilder.AppendLine($"<h1>Exception: {exception.Message}</h1>{exception.StackTrace}");
}
foreach (var header in Request.Headers)
{
var headerValues = string.Join(",", value: header.Value);
logBuilder.AppendLine($"{header.Key}: {headerValues}");
}
_logger.LogError(logBuilder.ToString());
if (id == null)
{
return View("Error");
}
switch (id.Value)
{
case 401:
case 403:
return View("AccessDenied");
case 404:
return View("NotFound");
default:
return View("Error");
}
}
} app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage();
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
using Microsoft.Extensions.Hosting;
app.UseStatusCodePagesWithReExecute("/Home/Error/{0}"); [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error(int? id)
{
string ErrorText = "";
ErrorText = (id.Value == 401 || id.Value == 403) ? "Access Denied"
: (id.Value == 404 ? "Not Found" : "Error Occured");
return View(new ErrorViewModel
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier,
ErrorId = id,
ErrorText = ErrorText
});
} <h1 class="text-danger">@Model.ErrorId : @Model.ErrorText</h1>
public class ErrorViewModel
{
public string RequestId { get; set; }
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
public int? ErrorId { get; set; }
public string ErrorText { get; set; }
} app.UseExceptionHandler(errorHandlingPath: "/MyControllerName/SomeActionMethodName");
public class Startup
{
private readonly IConfiguration _config;
public Startup(IConfiguration config)
{
_config = config;
}
public void ConfigureServices(IServiceCollection services)
{
var value = _config["key"];
}
public void Configure(IApplicationBuilder app, IConfiguration config)
{
var value = config["key"];
}
}