locked
Create Customize NotFound Method RRS feed

  • Question

  • User338455301 posted

    Hi 
    i want create methods for my customize example : 


    return NotFound();


    i write this method but i cant call it in other controller just i cant use in own  controller 

     public IActionResult NotFound1()
            {
                return View("NotFound");
            }




    how to create public method (exmaple : return Notfound()) ?

    Sunday, June 14, 2020 10:02 AM

All replies

  • User-217098811 posted

    Hi elahi1mahdi

    Maybe you can try it

    return Content(HttpStatusCode.NotFound, "Not Found");

    Hope this can help you

    Best regards

    yinqiu

    Monday, June 15, 2020 1:20 AM
  • User338455301 posted

    thanks but i want add function one line NotFound() , ...... and customzie it how to do it ?  

    Monday, June 15, 2020 6:29 AM
  • User711641945 posted

    Hi elahi1mahdi,

    It seems you want to call the NotFound1 action in the other controller,you could use `RedirectToAction("ActionName","ControllerName")` like below:

    return RedirectToAction("NotFound1","Home");

    Another way is make your controller inherits the controller which define the NotFound1 action:

    1.NotFound1 action:

    public class HomeController : Controller
    {
        private readonly IConfiguration configuration;
        public HomeController(IConfiguration config)
        {
            this.configuration = config;
        }
        public IActionResult NotFound1()
        {
            return View("NotFound");
        }
    }

    2.Another controller:

    public class AccountController : HomeController 
    {
        public AccountController(IConfiguration config) : base(config)
        {
        }
    
        // GET: /<controller>/
        public IActionResult Login()
        {
            //return View();
            return NotFound1();
        }
    }

    If you do not create any constructors,you could remove them by yourself.

    Best Regards,

    Rena

    Monday, June 15, 2020 8:40 AM
  • User338455301 posted

    thanks bit i want just call return NotFound1() in any controller without inherits with this way just use such as return NotFound()
    how to do it ?  

    Monday, June 15, 2020 10:06 AM
  • User475983607 posted

    thanks bit i want just call return NotFound1() in any controller without inherits with this way just use such as return NotFound()
    how to do it ?  

    NotFound() method is used in Web API to return a JSON response.  It does not return a View which, as far as I can tell, is what you are trying to do.

    Please read the standard error ASP.NET Core reference documentation and the blog below for information on configuring the exception handling middleware. 

    https://www.learnrazorpages.com/configuration/custom-errors  

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-3.1

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.statuscodepagesextensions.usestatuscodepageswithredirects?view=aspnetcore-3.1

    Keep in mind, the HTML response is completely under your control.

    Monday, June 15, 2020 10:39 AM
  • User338455301 posted

    its good way but have a problem 

    i use your way to do this work and now i can call NotFound1() in any controller 
    but in startup use this code to handle error by status code 

                app.UseStatusCodePagesWithReExecute("/" + CultureInfo.CurrentCulture.Name + "/errors/{0}");
    



    but because you use inheritance its show this error 

    AmbiguousMatchException: The request matched multiple endpoints. Matches:

    ModernProject.Controllers.ProductController.HandelError (itsaco_RefrencenetCoreCMS)
    ModernProject.Controllers.ErrorController.HandelError (itsaco_RefrencenetCoreCMS)
    AspCorePanelProject.Controllers.HomeController.HandelError (itsaco_RefrencenetCoreCMS)


    and its my controllers :

     private readonly IConfiguration _configuration;
            public ErrorController(IConfiguration configuration)
            {
                _configuration = configuration;
            }
            [Route("/{culture}/errors/{code}")]
            [Route("/errors/{code}")]
            public async Task<IActionResult> Error(string code)
            {
                if (code == "404")
                    return View("~/Views/Error/Page404.cshtml");
                else
                    return View("~/Views/Error/Page505.cshtml");
            }





    Tuesday, June 16, 2020 9:59 AM
  • User475983607 posted

    but because you use inheritance its show this error 

    No.  There are multiple Error Actions in your design that have the same URL.  Fix you code.

    Tuesday, June 16, 2020 11:38 AM
  • User338455301 posted

    no when i use this way i have this error 

    inheritance  from ErrorController  to cause of this error 

    when i remove this inheritance  its worked correctly  but i cant access to NotFound1()

    Thursday, June 18, 2020 6:43 AM
  • User711641945 posted

    Hi elahi1mahdi,

    You use UseStatusCodePagesWithReExecute middleware,it seems no need to use the Return NotFound1() to return the 404 error page.Why did you consist on using the custom NotFound method. Just you use the default method `NotFound()` and then it would hit the Error action and display the 404 error page.

    Best Regards,

    Rena

    Thursday, June 18, 2020 7:35 AM
  • User475983607 posted

    elahi1mahdi

    no when i use this way i have this error 

    inheritance  from ErrorController  to cause of this error 

    when i remove this inheritance  its worked correctly  but i cant access to NotFound1()

    Your design creates multiple Error actions.  Fix your code!

    Thursday, June 18, 2020 10:29 AM