Skip to content

Commit af0ed19

Browse files
Area in asp.net core mvc
1 parent f10d9df commit af0ed19

File tree

12 files changed

+181
-0
lines changed

12 files changed

+181
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
8+
namespace Webgentle.BookStore.Areas.Admin.Controllers
9+
{
10+
[Area("admin")]
11+
[Route("admin")]
12+
public class HomeController : Controller
13+
{
14+
// GET: HomeController
15+
[Route("")]
16+
public ActionResult Index()
17+
{
18+
return View();
19+
}
20+
21+
// GET: HomeController/Details/5
22+
[Route("details/{id}")]
23+
public ActionResult Details(int id)
24+
{
25+
return View(id);
26+
}
27+
28+
// GET: HomeController/Create
29+
public ActionResult Create()
30+
{
31+
return View();
32+
}
33+
34+
// POST: HomeController/Create
35+
[HttpPost]
36+
[ValidateAntiForgeryToken]
37+
public ActionResult Create(IFormCollection collection)
38+
{
39+
try
40+
{
41+
return RedirectToAction(nameof(Index));
42+
}
43+
catch
44+
{
45+
return View();
46+
}
47+
}
48+
49+
// GET: HomeController/Edit/5
50+
public ActionResult Edit(int id)
51+
{
52+
return View();
53+
}
54+
55+
// POST: HomeController/Edit/5
56+
[HttpPost]
57+
[ValidateAntiForgeryToken]
58+
public ActionResult Edit(int id, IFormCollection collection)
59+
{
60+
try
61+
{
62+
return RedirectToAction(nameof(Index));
63+
}
64+
catch
65+
{
66+
return View();
67+
}
68+
}
69+
70+
// GET: HomeController/Delete/5
71+
public ActionResult Delete(int id)
72+
{
73+
return View();
74+
}
75+
76+
// POST: HomeController/Delete/5
77+
[HttpPost]
78+
[ValidateAntiForgeryToken]
79+
public ActionResult Delete(int id, IFormCollection collection)
80+
{
81+
try
82+
{
83+
return RedirectToAction(nameof(Index));
84+
}
85+
catch
86+
{
87+
return View();
88+
}
89+
}
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@model int
2+
@{
3+
ViewData["Title"] = "Details";
4+
}
5+
6+
<h1>Details from Admin and value = @Model</h1>
7+
8+
<a asp-area="" asp-controller="Home" asp-action="Index">Go back to home</a>
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
@{
3+
ViewData["Title"] = "Index";
4+
}
5+
6+
<h1>Index from Admin</h1>
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@using Webgentle.BookStore.Models;
2+
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4+
@addTagHelper *, Webgentle.BookStore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace Webgentle.BookStore.Areas.Financial.Controllers
8+
{
9+
[Area("financial")]
10+
public class DashboardController : Controller
11+
{
12+
public IActionResult Index()
13+
{
14+
return View();
15+
}
16+
17+
public IActionResult Graph()
18+
{
19+
return View();
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace Webgentle.BookStore.Areas.Financial.Controllers
8+
{
9+
[Area("financial")]
10+
public class HomeController : Controller
11+
{
12+
public IActionResult Index()
13+
{
14+
return View();
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
@{
3+
ViewData["Title"] = "Graph";
4+
}
5+
6+
<h1>Graph from financial</h1>
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Scaffolding has generated all the files and added the required dependencies.
2+
3+
However the Application's Startup code may required additional changes for things to work end to end.
4+
Add the following code to the Configure method in your Application's Startup class if not already done:
5+
6+
app.UseMvc(routes =>
7+
{
8+
routes.MapRoute(
9+
name : "areas",
10+
template : "{area:exists}/{controller=Home}/{action=Index}/{id?}"
11+
);
12+
});

Webgentle.BookStore/Webgentle.BookStore/Startup.cs

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
100100
//endpoints.MapControllerRoute(
101101
// name: "Default",
102102
// pattern: "bookApp/{controller=Home}/{action=Index}/{id?}");
103+
104+
endpoints.MapControllerRoute(
105+
name: "MyArea",
106+
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
103107
});
104108
}
105109
}

Webgentle.BookStore/Webgentle.BookStore/Views/Shared/_header.cshtml

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
<li class="nav-item">
3838
<a class="nav-link text-dark" asp-controller="book" asp-action="AddNewBook">Add new book</a>
3939
</li>
40+
<li class="nav-item">
41+
<a class="nav-link text-dark" asp-area="Admin" asp-controller="Home" asp-action="Index">Admin</a>
42+
</li>
4043
</ul>
4144
</div>
4245
</div>

Webgentle.BookStore/Webgentle.BookStore/Webgentle.BookStore.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
</ItemGroup>
2121

2222
<ItemGroup>
23+
<Folder Include="Areas\Admin\Data\" />
24+
<Folder Include="Areas\Admin\Models\" />
25+
<Folder Include="Areas\Financial\Data\" />
26+
<Folder Include="Areas\Financial\Models\" />
2327
<Folder Include="wwwroot\books\cover\" />
2428
<Folder Include="wwwroot\books\gallery\" />
2529
<Folder Include="wwwroot\books\pdf\" />

Webgentle.BookStore/Webgentle.BookStore/Webgentle.BookStore.csproj.user

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
<View_SelectedScaffolderID>RazorViewScaffolder</View_SelectedScaffolderID>
1818
<View_SelectedScaffolderCategoryPath>root/View</View_SelectedScaffolderCategoryPath>
1919
<WebStackScaffolding_LayoutPageFile />
20+
<WebStackScaffolding_DependencyDialogWidth>600</WebStackScaffolding_DependencyDialogWidth>
2021
</PropertyGroup>
2122
</Project>

0 commit comments

Comments
 (0)