Home > database >  Change the home page based on a condition
Change the home page based on a condition

Time:11-09

I have a ASP.Net MVC application which returns a page called Customer as home page. So in the startup I have like below

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Customers}/{action=Index}/{id?}");
            });

But I have a requirement now that based on a criteria I need to show the Customer page as home page when it is true otherwise show another page. So in the the CustomersController I am not sure how to return to another page called (AppNotAvailable.cshtml)

   public async Task<IActionResult> Index(string sortOrder, string searchString,
                                           int? pageNumber, string currentFilter)
    {
        ViewData["CustomerParam"] = String.IsNullOrEmpty(sortOrder) ? "customer_desc" : "";
        ViewData["LocationParam"] = String.IsNullOrEmpty(sortOrder) ? "location_desc" : "";
        ViewData["CurrentFilter"] = searchString;
        ViewData["CurrentSort"] = sortOrder;

       if( Criteria == true)
        {
           return View(await PaginatedList<Customer>.CreateAsync(cust.AsNoTracking(), pageNumber ?? 1, pageSize));
         } 
         else
        {
         // How to return to the AppNotAvailable page
         }           
    }

Can we return two different views from the Controller

CodePudding user response:

That's simple:-

return View ('AppNotAvailable');

Or

return View("AppNotAvailable");
  • Related