<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fix asp net core error &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/fix-asp-net-core-error/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>ASP.NET Hosting &#124; Reviews &#124; Tips &#38; Tutorial</description>
	<lastBuildDate>Wed, 20 Jan 2021 04:45:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://topreviewhostingasp.net/wp-content/uploads/2017/01/cropped-trhaico-32x32.png</url>
	<title>fix asp net core error &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Fix 404 Error ASP.NET Core</title>
		<link>https://topreviewhostingasp.net/how-to-fix-404-error-asp-net-core/</link>
					<comments>https://topreviewhostingasp.net/how-to-fix-404-error-asp-net-core/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Wed, 20 Jan 2021 04:13:22 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[404 error asp net core]]></category>
		<category><![CDATA[asp.net core hosting]]></category>
		<category><![CDATA[best asp.net core hosting]]></category>
		<category><![CDATA[cheap asp.net core hosting]]></category>
		<category><![CDATA[fix asp net core error]]></category>
		<category><![CDATA[how to fix 404 error]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=2848</guid>

					<description><![CDATA[I have checked that some users experience this error 404 when deploying ASP.NET Core applications and I decide to write this tutorial. I hope it will fix the issue. Fix 404 Error ASP.NET Core I found 2 ways to handle 404 error. In fact using these solution you can handle any HTTP status code errors. [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>I have checked that some users experience this error 404 when deploying ASP.NET Core applications and I decide to write this tutorial. I hope it will fix the issue.</p>



<h2 class="wp-block-heading">Fix 404 Error ASP.NET Core</h2>



<p>I found 2 ways to handle 404 error. In fact using these solution you can handle any HTTP status code errors. To handle the error, both the solution are using <code>configure()</code> method of <strong>Startup.cs</strong> class. For those who are not aware about Startup.cs, it is entry point for application itself.</p>

<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.asphostportal.com"><img decoding="async" width="468" height="60" class="wp-image-2850 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2021/01/banner-affiliate-ahp-05.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/01/banner-affiliate-ahp-05.png 468w, https://topreviewhostingasp.net/wp-content/uploads/2021/01/banner-affiliate-ahp-05-300x38.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/01/banner-affiliate-ahp-05-50x6.png 50w" sizes="(max-width: 468px) 100vw, 468px" /></a></figure>
</div>

<h3 class="wp-block-heading">Solution 1</h3>



<p>Now coming back to our solution 1, within configure method define a custom middleware via <code>app.Use</code> which checks for status code value in response object. And if is 404 then it redirects to Home controller. See highlighted code.</p>
<p>

</p>
<pre class="wp-block-code"><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseApplicationInsightsRequestTelemetry();
    app.Use(async (context, next) =&gt;
    {
        await next();
        if (context.Response.StatusCode == 404)
        {
            context.Request.Path = "/Home"; 
            await next();
        }
    });

    app.UseIISPlatformHandler(options =&gt; options.AuthenticationDescriptions.Clear());
    app.UseApplicationInsightsExceptionTelemetry();
    app.UseStaticFiles();
    app.UseIdentity();
    // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715
    app.UseMvc(routes =&gt;
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}</code></pre>
<p>

</p>
<pre class=""></pre>
<p>

</p>
<h3 class="wp-block-heading">Solution 2</h3>
<p>

</p>
<p>The other solution is to use a built-in middlware <em>StatusCodePagesMiddleware</em>. This middleware can be used to handle the response status code is between 400 and 600. This middleware allows to return a generic error response or allows you to also redirect to any controller action or another middleware. See below all different variations of this middleware.</p>
<p>

</p>
<pre class="wp-block-code"><code>app.UseStatusCodePages();

// app.UseStatusCodePages(context =&gt; context.HttpContext.Response.SendAsync("Handler, status code: " + context.HttpContext.Response.StatusCode, "text/plain"));
// app.UseStatusCodePages("text/plain", "Response, status code: {0}");
// app.UseStatusCodePagesWithRedirects("~/errors/{0}"); // PathBase relative
// app.UseStatusCodePagesWithRedirects("~/errors/{0}"); // PathBase relative
// app.UseStatusCodePagesWithRedirects("/base/errors/{0}"); // Absolute
// app.UseStatusCodePages(builder =&gt; builder.UseWelcomePage());
// app.UseStatusCodePagesWithReExecute("/errors/{0}");</code></pre>
<p>

</p>
<p>Now to handle the 404 error, we shall use <code>app.UseStatusCodePagesWithReExecute</code> which accepts a path where you wish to redirect.</p>
<p>

</p>
<pre class="wp-block-preformatted">app.UseStatusCodePagesWithReExecute("/Home/Errors/{0}");</pre>
<p>

</p>
<p>So we are redirecting here to Home Controller and Errors action method.</p>
<p>

</p>
<pre class="wp-block-code"><code>public IActionResult Errors(string errCode)
{
    ViewData["ErrorID"] = "The following error " + errCode + " occured";
    return View("~/Views/Shared/Error.cshtml");
}</code></pre>
<p>

</p>
<p>The <code><strong>{0}</strong></code> is nothing but the HTTP status error code. Below is the implementation of Errors action method.It adds the status code in ViewData and then returns to Error.cshtml shared view. You can also return to specific error page based on the error code.</p>
<p>

</p>
<pre class="wp-block-code"><code>public IActionResult Errors(string errCode) 
{
    if (errCode == "500" | errCode == "404") 
    {
      return View($"~/Views/Home/Error/{errCode}.cshtml"); 
    }

    return View("~/Views/Shared/Error.cshtml"); 
}</code></pre>
<p>

</p>
<h2 class="wp-block-heading">Conclusion</h2>
<p>

</p>
<p>So, if the error code is 500 or 404 then return to Home/Error/500.cshtml or 404.cshtml.</p>
<p>

</p>
<p>You must have seen on many websites, forums about <code><strong>app.UseErrorPage();</strong></code> to handle the errors. But <strong>this is no longer available with RC1 release</strong> of ASP.NET Core 1.0. This was available until beta 5 or 6.</p>
<p></p>]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/how-to-fix-404-error-asp-net-core/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
