I have an Area called "admin" and there is 2 controllers inside the Controllers folders. One of them is SlideController. SlideController have multiple actions, two of them have views. They are Index and New actions.
The problem is in the admin panel layout. When ı try to click "Slide" it routes me to "localhost54664/Slide" but it should be /admin/Slide . "New" action have same problem.
Codes:
SlideController
using Evim.BL.Repositories;
using Evim.DAL.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Evim.UI.Areas.admin.Controllers
{
[Area("admin"), Authorize]
public class SlideController : Controller
{
IRepository<Slide> repoSlide;
public SlideController(IRepository<Slide> _repoSlide)
{
repoSlide = _repoSlide;
}
public IActionResult Index()
{
return View(repoSlide.GetAll().OrderByDescending(x => x.ID));
}
public IActionResult New()
{
return View();
}
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> InsertAsync(Slide model)
{
if (ModelState.IsValid)
{
if (Request.Form.Files.Any())
{
if (!Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "img", "slide")))
{
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "img", "slide"));
}
string dosyaAdi = Request.Form.Files["Picture"].FileName;
using (FileStream stream = new FileStream(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "img", "slide", dosyaAdi), FileMode.Create))
{
await Request.Form.Files["Picture"].CopyToAsync(stream);
}
model.Picture = "/img/slide/" + dosyaAdi;
}
repoSlide.Add(model);
return RedirectToAction("Index");
}
else return RedirectToAction("New");
}
public IActionResult Edit(int id)
{
Slide slide = repoSlide.GetBy(x => x.ID == id);
if (slide != null) return View(slide);
else return RedirectToAction("Index");
}
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Slide model)
{
if (ModelState.IsValid)
{
if (Request.Form.Files.Any())
{
if (!Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "img", "slide")))
{
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "img", "slide"));
}
string dosyaAdi = Request.Form.Files["Picture"].FileName;
using (FileStream stream = new FileStream(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "img", "slide", dosyaAdi), FileMode.Create))
{
await Request.Form.Files["Picture"].CopyToAsync(stream);
}
model.Picture = "/img/slide/" + dosyaAdi;
}
repoSlide.Update(model);
return RedirectToAction("Index");
}
else return RedirectToAction("New");
}
public IActionResult Delete(int id)
{
Slide slide = repoSlide.GetBy(x => x.ID == id);
if (slide != null) repoSlide.Delete(slide);
return RedirectToAction("Index");
}
}
}
_Layout
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="images/favicon.ico" type="image/ico" />
<title>Gentelella Alela!</title>
<!-- Bootstrap -->
<link href="/admin/vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Font Awesome -->
<link href="/admin/vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet">
<!-- NProgress -->
<link href="/admin/vendors/nprogress/nprogress.css" rel="stylesheet">
<!-- iCheck -->
<link href="/admin/vendors/iCheck/skins/flat/green.css" rel="stylesheet">
<!-- bootstrap-progressbar -->
<link href="/admin/vendors/bootstrap-progressbar/css/bootstrap-progressbar-3.3.4.min.css" rel="stylesheet">
<!-- JQVMap -->
<link href="/admin/vendors/jqvmap/dist/jqvmap.min.css" rel="stylesheet" />
<!-- bootstrap-daterangepicker -->
<link href="/admin/vendors/bootstrap-daterangepicker/daterangepicker.css" rel="stylesheet">
<!-- Custom Theme Style -->
<link href="/admin/custom.min.css" rel="stylesheet">
</head>
<body class="nav-md">
<div class="container body">
<div class="main_container">
<div class="col-md-3 left_col">
<div class="left_col scroll-view">
<div class="navbar nav_title" style="border: 0;">
<a href="index.html" class="site_title"><i class="fa fa-paw"></i> <span>Gentelella Alela!</span></a>
</div>
<div class="clearfix"></div>
<!-- menu profile quick info -->
<div class="profile clearfix">
<div class="profile_pic">
<img src="images/img.jpg" alt="..." class="img-circle profile_img">
</div>
<div class="profile_info">
<span>Welcome,</span>
<h2>@User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value</h2>
</div>
</div>
<!-- /menu profile quick info -->
<br />
<!-- sidebar menu -->
<div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
<div class="menu_section">
<h3>General</h3>
<ul class="nav side-menu">
<li>
<a asp-controller="Home" asp-action="Index"><i class="fa fa-home"></i> Ana Sayfa </a>
</li>
<li>
<a asp-controller="Slide" asp-action="Index"><i class="fa fa-clone"></i> Slaytlar </a>
<ul class="nav child_menu">
<li><a href="form.html">General Form</a></li>
<li><a href="form_advanced.html">Advanced Components</a></li>
<li><a href="form_validation.html">Form Validation</a></li>
<li><a href="form_wizards.html">Form Wizard</a></li>
<li><a href="form_upload.html">Form Upload</a></li>
<li><a href="form_buttons.html">Form Buttons</a></li>
</ul>
</li>
<li>
<a asp-controller="Category" asp-action="Index"><i class="fa fa-desktop"></i> Kategoriler <span class="fa fa-chevron-down"></span></a>
</li>
<li>
<a asp-controller="Product" asp-action="Index"><i class="fa fa-desktop"></i> Ürünler </a>
</li>
<li>
<a asp-controller="Brand" asp-action="Index"><i class="fa fa-desktop"></i> Markalar </a>
</li>
<li>
<a><i class="fa fa-table"></i> Tables <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="tables.html">Tables</a></li>
<li><a href="tables_dynamic.html">Table Dynamic</a></li>
</ul>
</li>
<li>
<a><i class="fa fa-bar-chart-o"></i> Data Presentation <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="chartjs.html">Chart JS</a></li>
<li><a href="chartjs2.html">Chart JS2</a></li>
<li><a href="morisjs.html">Moris JS</a></li>
<li><a href="echarts.html">ECharts</a></li>
<li><a href="other_charts.html">Other Charts</a></li>
</ul>
</li>
<li>
<a><i class="fa fa-clone"></i>Layouts <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="fixed_sidebar.html">Fixed Sidebar</a></li>
<li><a href="fixed_footer.html">Fixed Footer</a></li>
</ul>
</li>
</ul>
</div>
<div class="menu_section">
<h3>Live On</h3>
<ul class="nav side-menu">
<li>
<a><i class="fa fa-bug"></i> Additional Pages <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="e_commerce.html">E-commerce</a></li>
<li><a href="projects.html">Projects</a></li>
<li><a href="project_detail.html">Project Detail</a></li>
<li><a href="contacts.html">Contacts</a></li>
<li><a href="profile.html">Profile</a></li>
</ul>
</li>
<li>
<a><i class="fa fa-windows"></i> Extras <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="page_403.html">403 Error</a></li>
<li><a href="page_404.html">404 Error</a></li>
<li><a href="page_500.html">500 Error</a></li>
<li><a href="plain_page.html">Plain Page</a></li>
<li><a href="login.html">Login Page</a></li>
<li><a href="pricing_tables.html">Pricing Tables</a></li>
</ul>
</li>
<li>
<a><i class="fa fa-sitemap"></i> Multilevel Menu <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="#level1_1">Level One</a>
<li>
<a>Level One<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="sub_menu">
<a href="level2.html">Level Two</a>
</li>
<li>
<a href="#level2_1">Level Two</a>
</li>
<li>
<a href="#level2_2">Level Two</a>
</li>
</ul>
</li>
<li>
<a href="#level1_2">Level One</a>
</li>
</ul>
</li>
<li><a href="javascript:void(0)"><i class="fa fa-laptop"></i> Landing Page <span class="label label-success pull-right">Coming Soon</span></a></li>
</ul>
</div>
</div>
<!-- /sidebar menu -->
<!-- /menu footer buttons -->
<div class="sidebar-footer hidden-small">
<a data-toggle="tooltip" data-placement="top" title="Settings">
<span class="glyphicon glyphicon-cog" aria-hidden="true"></span>
</a>
<a data-toggle="tooltip" data-placement="top" title="FullScreen">
<span class="glyphicon glyphicon-fullscreen" aria-hidden="true"></span>
</a>
<a data-toggle="tooltip" data-placement="top" title="Lock">
<span class="glyphicon glyphicon-eye-close" aria-hidden="true"></span>
</a>
<a data-toggle="tooltip" data-placement="top" title="Logout" asp-controller="Home" asp-action="LogOut">
<span class="glyphicon glyphicon-off" aria-hidden="true"></span>
</a>
</div>
<!-- /menu footer buttons -->
</div>
</div>
<!-- top navigation -->
<div class="top_nav">
<div class="nav_menu">
<div class="nav toggle">
<a id="menu_toggle"><i class="fa fa-bars"></i></a>
</div>
<nav class="nav navbar-nav">
<ul class=" navbar-right">
<li class="nav-item dropdown open" style="padding-left: 15px;">
<a href="javascript:;" class="user-profile dropdown-toggle" aria-haspopup="true" id="navbarDropdown" data-toggle="dropdown" aria-expanded="false">
<img src="images/img.jpg" alt="">@User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value
</a>
<div class="dropdown-menu dropdown-usermenu pull-right" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="javascript:;"> Profile</a>
<a class="dropdown-item" href="javascript:;">
<span class="badge bg-red pull-right">50%</span>
<span>Settings</span>
</a>
<a class="dropdown-item" href="javascript:;">Help</a>
<a class="dropdown-item" asp-controller="Home" asp-action="LogOut"><i class="fa fa-sign-out pull-right"></i> Log Out</a>
</div>
</li>
<li role="presentation" class="nav-item dropdown open">
<a href="javascript:;" class="dropdown-toggle info-number" id="navbarDropdown1" data-toggle="dropdown" aria-expanded="false">
<i class="fa fa-envelope-o"></i>
<span class="badge bg-green">6</span>
</a>
<ul class="dropdown-menu list-unstyled msg_list" role="menu" aria-labelledby="navbarDropdown1">
<li class="nav-item">
<a class="dropdown-item">
<span class="image"><img src="images/img.jpg" alt="Profile Image" /></span>
<span>
<span>John Smith</span>
<span class="time">3 mins ago</span>
</span>
<span class="message">
Film festivals used to be do-or-die moments for movie makers. They were where...
</span>
</a>
</li>
<li class="nav-item">
<a class="dropdown-item">
<span class="image"><img src="images/img.jpg" alt="Profile Image" /></span>
<span>
<span>John Smith</span>
<span class="time">3 mins ago</span>
</span>
<span class="message">
Film festivals used to be do-or-die moments for movie makers. They were where...
</span>
</a>
</li>
<li class="nav-item">
<a class="dropdown-item">
<span class="image"><img src="images/img.jpg" alt="Profile Image" /></span>
<span>
<span>John Smith</span>
<span class="time">3 mins ago</span>
</span>
<span class="message">
Film festivals used to be do-or-die moments for movie makers. They were where...
</span>
</a>
</li>
<li class="nav-item">
<a class="dropdown-item">
<span class="image"><img src="images/img.jpg" alt="Profile Image" /></span>
<span>
<span>John Smith</span>
<span class="time">3 mins ago</span>
</span>
<span class="message">
Film festivals used to be do-or-die moments for movie makers. They were where...
</span>
</a>
</li>
<li class="nav-item">
<div class="text-center">
<a class="dropdown-item">
<strong>See All Alerts</strong>
<i class="fa fa-angle-right"></i>
</a>
</div>
</li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
<!-- /top navigation -->
<!-- page content -->
@RenderBody()
<!-- /page content -->
<!-- footer content -->
<footer>
<div class="pull-right">
Gentelella - Bootstrap Admin Template by <a href="https://colorlib.com">Colorlib</a>
</div>
<div class="clearfix"></div>
</footer>
<!-- /footer content -->
</div>
</div>
<!-- jQuery -->
<script src="/admin/vendors/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="/admin/vendors/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<!-- FastClick -->
<script src="/admin/vendors/fastclick/lib/fastclick.js"></script>
<!-- NProgress -->
<script src="/admin/vendors/nprogress/nprogress.js"></script>
<!-- Chart.js -->
<script src="/admin/vendors/Chart.js/dist/Chart.min.js"></script>
<!-- gauge.js -->
<script src="/admin/vendors/gauge.js/dist/gauge.min.js"></script>
<!-- bootstrap-progressbar -->
<script src="/admin/vendors/bootstrap-progressbar/bootstrap-progressbar.min.js"></script>
<!-- iCheck -->
<script src="/admin/vendors/iCheck/icheck.min.js"></script>
<!-- Skycons -->
<script src="/admin/vendors/skycons/skycons.js"></script>
<!-- Flot -->
<script src="/admin/vendors/Flot/jquery.flot.js"></script>
<script src="/admin/vendors/Flot/jquery.flot.pie.js"></script>
<script src="/admin/vendors/Flot/jquery.flot.time.js"></script>
<script src="/admin/vendors/Flot/jquery.flot.stack.js"></script>
<script src="/admin/vendors/Flot/jquery.flot.resize.js"></script>
<!-- Flot plugins -->
<script src="/admin/vendors/flot.orderbars/js/jquery.flot.orderBars.js"></script>
<script src="/admin/vendors/flot-spline/js/jquery.flot.spline.min.js"></script>
<script src="/admin/vendors/flot.curvedlines/curvedLines.js"></script>
<!-- DateJS -->
<script src="/admin/vendors/DateJS/build/date.js"></script>
<!-- JQVMap -->
<script src="/admin/vendors/jqvmap/dist/jquery.vmap.js"></script>
<script src="/admin/vendors/jqvmap/dist/maps/jquery.vmap.world.js"></script>
<script src="/admin/vendors/jqvmap/examples/js/jquery.vmap.sampledata.js"></script>
<!-- bootstrap-daterangepicker -->
<script src="/admin/vendors/moment/min/moment.min.js"></script>
<script src="/admin/vendors/bootstrap-daterangepicker/daterangepicker.js"></script>
<script src="/admin/jquery.validate.min.js"></script>
<script src="/admin/jquery.validate.unobtrusive.min.js"></script>
<!-- Custom Theme Scripts -->
<script src="/admin/custom.min.js"></script>
@RenderSection("customScripts", required: false)
</body>
</html>
**
Program.cs**
using Evim.DAL.Content;
using Evim.BL.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.Cookies;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<SQLContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("CS1")));
builder.Services.AddScoped(typeof(IRepository<>), typeof(SQLRepository<>));
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(opt =>
{
opt.ExpireTimeSpan = TimeSpan.FromMinutes(60);
opt.LoginPath = "/admin/login"; //yetkisi olmadan giri� yapmaya �al��t���nda
opt.LogoutPath = "/admin/logout"; // s�re doldu�unda gidece�i sayfa
}
);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) app.UseStatusCodePagesWithRedirects("/hata/{0}");
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication(); //kimlik do�rulama
app.UseAuthorization();
app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(name: "areas", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.Run();
When ı try first time ı realize my "admin" folder is not "area" then ı create a area called admin and ı try again but it s not working and ı have same problem.
2
Answers
I solved like this, add route manually for every action.
From Add Area route we can see:
Try to put the area route before default route like:
Besides, set
asp-area
to "Slide" prefixes the directory Areas/Slide to the routes of the associated controllers :result:
You can read Anchor Tag Helper usage-in-mvc to know more.