skip to Main Content

I send data from Index.cshtml to my controller. Then I want to redirect from the controller to the index.cshtml page inside the Admin folder (those two are two different index.cstml files). I added a screenshot of the file hierarchy. Please help me to solve this.

I tried this, but it’s not working

public IActionResult Login([FromBody] Users user)
{
    if (user.UserName.Equals("admin") & user.Password.Equals("admin"))
    {
        return LocalRedirect("/Admin/Index"); // redirect to index page in Admin folder
    }
    else
        return RedirectToPage("Error");
}

Folder structure:

folder structure for better understand

This is startup page

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using PracticalTest.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PracticalTest.UIs
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["defaultConnection"]));
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
            });

        }
    }
}

AdminController

using Microsoft.AspNetCore.Mvc;
using PracticalTest.Application.Admin;
using PracticalTest.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PracticalTest.UIs.Controllers
{
    [Route("[controller]")]
    public class AdminController : Controller
    {
        private ApplicationDbContext _ctx;
        public AdminController(ApplicationDbContext ctx)
        {
            _ctx = ctx;
        }

        [HttpGet("Items")]
        public IActionResult GetItems() =>Ok( new GetItems(_ctx).Do());
        [HttpGet("Users")]
        public IActionResult GetUsers() => Ok(new GetUsers(_ctx).Do());

        [HttpGet("Items/{id}")]
        public IActionResult GetItem(int id) =>Ok(new GetItem(_ctx).Do(id));


        [HttpGet("Users/{id}")]
        public IActionResult GetUser(int id) => Ok(new GetUser(_ctx).Do(id));

        [HttpPost("Items")]
        public async Task<IActionResult> CreateItem([FromBody]CreateItem.Requst requst) =>Ok((await new CreateItem(_ctx).Do(requst)));
        [HttpPost("Users")]
        public async Task<IActionResult> CreateUser([FromBody] CreateUser.Requst requst) => Ok((await new CreateUser(_ctx).Do(requst)));

        [HttpDelete("Items/{id}")] 
        public async Task<IActionResult> DeleteItem(int id) =>Ok((await new DeleteItem(_ctx).Do(id)));

        [HttpDelete("Users/{id}")]
        public async Task<IActionResult> DeleteUser(int id) => Ok((await new DeleteUser(_ctx).Do(id)));

        [HttpPut("Items")]
        public async Task<IActionResult> UpdateItem([FromBody]UpdateItem.Request rq) =>Ok((await new UpdateItem(_ctx).Do(rq)));
        [HttpPut("Users")]
        public async Task<IActionResult> UpdateUser([FromBody] UpdateUser.Request rq) => Ok((await new UpdateUser(_ctx).Do(rq)));

       
    }
}

2

Answers


  1. How about this?

    if (user.UserName.Equals("admin") & user.Password.Equals("admin"))
    {
        // redirect to index page in Admin folder
        return RedirectToPage("Admin/Index"); 
    }
    ....
    
    Login or Signup to reply.
  2. If you want to go to an action, you would pass the action as the first argument and the controller as the second argument.

    RedirectToAction("Index", "Admin");
    

    If you just want to redirect to the page itself, you would go to the directory that the view is located. Such as return View("~/Views/Chats/Index.cshtml");

    return View("~/Views/Admin/Index.cshtml");
    

    In the code above you would replace the argument with wherever your page is located.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search