Asp.net核心MVC UseStatusCodePagesWithRedirects本地主机重定向次数过多
不确定我是否遗漏了一些明显的东西,或者这可能是一个bug 在startup.cs的方法Configure中,我有“UseStatusCodePagesWithRedirects”。我最后放了一个静态文件,希望它能有所不同。之前我在Home controller中使用错误方法,但最终结果是相同的 以下是我的完整配置方法:Asp.net核心MVC UseStatusCodePagesWithRedirects本地主机重定向次数过多,asp.net,asp.net-mvc,asp.net-core,Asp.net,Asp.net Mvc,Asp.net Core,不确定我是否遗漏了一些明显的东西,或者这可能是一个bug 在startup.cs的方法Configure中,我有“UseStatusCodePagesWithRedirects”。我最后放了一个静态文件,希望它能有所不同。之前我在Home controller中使用错误方法,但最终结果是相同的 以下是我的完整配置方法: public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory lo
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStatusCodePagesWithRedirects("~/errors/{0}.html");
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
我有一个错误
目录,只有一个静态404.html文件
如果我故意创建一个404错误,比如在URL中的“Home”之后键入“joe”,我会得到
The localhost page isn’t working
localhost redirected you too many times.
检查边缘中的F12/Network,我看到它被重定向到正确的页面,但次数太多了
所以,我想我的问题是:一旦正确的错误页面被点击,我如何更改响应代码?但我觉得我不应该这么做
提前感谢你帮助我了解这里发生的事情
顺便说一下,如果我简单地把
app.UseStatusCodePages()代码>它确实有效,但这很难看….以下是本网站上的内容:
您必须将其放入启动中。cs->Configure必须将其放入:
// Will give error status code
//app.UseStatusCodePages();
//app.UseStatusCodePagesWithRedirects("/Error/{0}");
app.UseStatusCodePagesWithReExecute("/Home/Error", "?statusCode={0}");
然后,您必须在主控制器中输入以下代码:
public IActionResult Error(int? statusCode = null)
{
if (statusCode.HasValue)
{
if (statusCode == 404 || statusCode == 500)
{
var viewName = statusCode.ToString();
return View(viewName);
}
}
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
最后,您将向视图->主文件夹添加两个新视图,即404.cshtml和500.cshtml
在404.cshtml中,可以放置以下内容:
@{
ViewBag.Title = "Page Not Found" ;
}
<h1>(404) Page Not Found.</h1>
<p>This page doesn't exist.</p>
<p>Please, contact the support by emailing them this page error.</p>
<p>To find the available contacts go to: @Html.ActionLink("Contacts", "Contact", "Home")</p>
@{
ViewBag.Title=“未找到页面”;
}
(404)找不到页面。
此页面不存在
请通过电子邮件将此页面错误发送给支持人员,与他们联系
要查找可用联系人,请转到:@Html.ActionLink(“联系人”、“联系人”、“主页”)
在500.cshtml的内部:
@{
ViewBag.Title = "(500) An Error Occurred";
}
<h1>(500) An Error Occurred.</h1>
<p>Something went wrong.</p>
<p>Please, contact the support by emailing them this page error.</p>
<p>To find the available contacts go to: @Html.ActionLink("Contacts", "Contact", "Home")</p>
@{
ViewBag.Title=“(500)发生错误”;
}
(500)发生了一个错误。
出了点问题
请通过电子邮件将此页面错误发送给支持人员,与他们联系
要查找可用联系人,请转到:@Html.ActionLink(“联系人”、“联系人”、“主页”)
任何其他错误都会执行default error.cshtml模板(我使用的是Microsoft default error.cshtml)。目录error
或errors
与代码类似吗?“我有一个错误目录,只有一个静态404.html文件。”该目录在wwroot内吗?该目录是errors,但不在wwwroot内!现在让我改变一下,看看它是否能解决这个问题。这就是问题所在,StaticFiles中间件试图在那里找到它,并抛出404 againOK,它确实有效,在wwwroot中移动目录和文件。我更喜欢使用控制器和视图,但由于默认的异常处理程序使用的是“已存在的主页/错误”,我想我必须编写自己的来处理这些页面错误。