locked
How can I redirect to the 404 page in a controller? RRS feed

  • 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

All replies