Issue
This Content is from Stack Overflow. Question asked by AdamD
when I create an admin folder and then add an empty razor page and do /admin, it tells me the page cannot be found. However I have another User folder and when I do /User it works.
These folders are listed in a Views folder.
I tried duplicating the User folder and renaming it to Admin but it didn’t work.
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.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Admin";
});
//services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<ApplicationDbContext>(options=>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnectionSqlite")));
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
services.AddTransient<IEmailSender, MailJetEmailSender>();
services.AddControllersWithViews();
}
// 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("/Home/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.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
Solution
If you have a view named Index.cshtml
in the path Views\Admin\Index.cshtml
then you should create a Controller
named AdminController
:
public class AdminController : Controller
{
public IActionResult Index()
{
return View();
}
}
Notice the method name (Index
) is the same as the view name.
If you want to have different method name and view name then you need to specify the view name when you return the view:
public class AdminController : Controller
{
public IActionResult View()
{
return View("Index");
}
}
This Question was asked in StackOverflow by AdamD and Answered by Dimitris Maragkos It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.