Answered by:
How can I redirect to the 404 page in a controller?

Question
-
User1052024640 posted
I made a 404page by the UseStatusCodePagesWithReExecute like this:
app.UseStatusCodePagesWithReExecute(/StatusCode/{0});
And here is the code of StatusCodeController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace Test.Controllers
{
public class StatusCodeController : Controller
{[HttpGet(/StatusCode/{statusCode})]
[HttpGet({culture}/StatusCode/{statusCode})]
public IActionResult Index(int statusCode)
{
return View(statusCode);
}
}
}
The 404 page works well.
Now I have another controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Test.Models;
using Microsoft.Extensions.Localization;namespace Test.Controllers
{
[Route({culture}/[controller])]
public class ProductController : Controller
{[Route({id}.html)]
public ViewResult Product(string id) {
Models.ProductModel PM = new ProductModel();
switch (id)
{
case T1: {
/// ............................///
break;
}
case T2:
{
/// ............................///
break;
}
default: {
return View(/StatusCode/404);
}
}
return View(PM);
}
}
}I want to redirect to the 404 pages while the Id is in default option of the switch. However, it won't redirect to the 404 pages but throws a
'InvalidOperationException: The view '/StatusCode/404' was not found. The following locations were searched:/StatusCode/404'
error.
I wonder if the viewname is wrong and I tried many times but failed. How can I solve it? Thank you.Saturday, September 14, 2019 1:02 PM
Answers
-
User1634355159 posted
Hi mywatermelon,
In your code you use “return View("/StatusCode/404")”,but the String in the return view(String) could only be the name or path of the view that is rendered to the response,and your "/StatusCode/404" does not seem to be it in you project.
You could use RedirectToAction(String, String, Object, String) to go to “/StatusCode/404” ,just like the following code:
public ActionResult Product(string id) { Model PM = new Model(); switch (id) { case "1": { /// ............................/// break; } case "2": { /// ............................/// break; } default: { return RedirectToAction("Index", "StatusCode", new { statusCode = 404 }); } } return View(PM); }
Best Regards ,
Lewis Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 16, 2019 6:03 AM
All replies
-
User1634355159 posted
Hi mywatermelon,
In your code you use “return View("/StatusCode/404")”,but the String in the return view(String) could only be the name or path of the view that is rendered to the response,and your "/StatusCode/404" does not seem to be it in you project.
You could use RedirectToAction(String, String, Object, String) to go to “/StatusCode/404” ,just like the following code:
public ActionResult Product(string id) { Model PM = new Model(); switch (id) { case "1": { /// ............................/// break; } case "2": { /// ............................/// break; } default: { return RedirectToAction("Index", "StatusCode", new { statusCode = 404 }); } } return View(PM); }
Best Regards ,
Lewis Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 16, 2019 6:03 AM -
User1052024640 posted
Thank you. It works.
Monday, September 16, 2019 6:16 AM