Lập trình ASP.NET MVC – Bài 3: Cách tạo vùng Admin bằng Area | Create area for admin | TEDU
Mã nguồn bài học Onlineshop:
Mã nguồn CMS ASP.NET MVC:
Trong bài này chúng ta sẽ tìm hiểu cách tạo vùng Admin cho trang web bằng cách sử dụng tính năng Area trong ASP.NET MVC.
Ứng dụng thực tiễn dùng để tạo ra phần quản trị cho website có layout riêng. Ngoài ra chúng ta cũng có thể tạo nhiều vùng trong ứng dụng theo yêu cầu của từng dự án thực tế.
hãy sửa thêm thuộc tính namespace vào để phân biệt 2 controller cùng tên ở bên trong và bên ngoài nhé:
routes.MapRoute(
name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional },
namespaces: new string[] { “WebApp.Controllers” }
);
Trong namespace mình đưa ra cái namespace của controller bên ngoài, như thế nó sẽ hiểu, thứ nữa là bạn phải khai báo namespace cho tất các các dòng khác
▶ More information about TEDU:
Website:
Hotline: 0966 036 626
Email: tedu.international@gmail.com
▶ CLICK TO SUBSCRIBE:
#tedu #aspnetmvc
DO NOT REUPLOAD
Nguồn: https://technewsgator.com
Xem thêm bài viết khác: https://technewsgator.com/lap-trinh/
Xem thêm Bài Viết:
- [Khóa học lập trình C# Cơ bản] – Bài 3: Nhập xuất cơ bản | HowKteam
- Lập Trình Joomla – Bài 185: Xây Dựng Cấu Trúc Template 01
- Phần 2 – Demo game Snake [Lập trình game cơ bản]
- [Lập trình C/C++] Bài 1_1. Hướng dẫn các thao tác căn bản trên Visual Studio 2013 và 2015(Phần 1)
- Tự học PHP – Bài 100 Làm việc với Session – Phần 1
Em yêu anh. .ahihi
Ae ai sử dụng visua 2019 mà không có mục Areas thì tạo trước thư mục Areas rồi nhấn chuột phải chọn add->Areas.
Visual studio 2019 lại không có phần add area nhỉ
cho em hỏi ngoài lề. Em add ko đc các reference , tại sao vậy anh. các reference nó để dấu chấm thang qua bên ngoài
Hay !
AE cho hỏi trên macos không có add area thì sao nhỉ :))
bài nay hơi khó hiểu anh ạ
lúc mới tạo project , chọn MVC 5 được ko a ? có khác quá nhiều ko a ??
có thể cho Phương xin database MDF của OnlineShop không Toàn? TEDU Channel
minh sài visual studio 2012 sao tạo area chạy không đc vậy cần thêm thư viện gì vậy
http://www.mediafire.com/file/o3bdsf9mb3vk94a/OnlineShop_52.zip
Mỗi video click cho bạn 1 quảng cáo, cảm ơn bạn rất nhiều!
Thầy ơi em dùng visua 2017 tạo Area mà khai báo đường dẫn nó không hiểu
Trong dòng code : new { action = "Index", controllers="Home", id = UrlParameter.Optional } thì viết hoa Controller vẫn chạy được.
Cảm ơn bạn tedu rất nhiều !!
Bạn nói hơi nhanh :v Cảm ơn bạn rất nhiều
Mình tạo giống anh rồi. Nhưng khi vào Index của phần user nó lại báo lỗi. Mình vào http://localhost:55439/Home/Index thì bị lỗi nhưng vào http://localhost:55439/Admin/Home/Index thì okey
a ơi khi tạo admin e bị báo lỗi Could not find a part of the path: "c:program files (x86) microsoft visual studio 12common7ideitem templatescachecsharpwebmvc4mvcarmvcareaitem templatev4.1.csmvcareaitem templates.cs.vstemplate" phải sửa như thế nào ạ?
anh ơi cho e hỏi lỗi này là gì và cách khác phục sao vậy ạ.
Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
The request for 'Home' has found the following matching controllers:
Website.Areas.Admin.Controllers.HomeController.HomeController
Website.Controllers.HomeController.HomeController
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
lỗi gì vậy mọi người ??????Lên google rồi mà chưa đc. Ai sửa hộ tui với!!!!!!!!!!!!
Cám ơn!
Anh ơi.. em thêm vào nhứ anh nó vẫn lỗi như vậy anh ơi.. chỉ mình với
Lỗi như thế này là như thế nào : Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
Em có tạo như anh nhưng khi trả về actionresult return RedirectToAction("Index", "New", new { area = "" }); về 1 controller ở thư mục controller mặc định khi tạo project thì nó báo lỗi http://localhost:53808/Admin/New/Index ! cho dù có trả về gì thì vẫn có Admin trên đường dẫn đẫn đến ko tìm ra hàm ! Giờ em muốn gọi đến Controller ở thư mục mặc định thì phải làm thế nào ạ ?
Bạn ơi! bạn thao tác lẹ quá, bạn có thể thao tác chậm lại 1 chút nữa được không.
Cách sửa lỗi Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
thì:
B1 : Mở file HomeController ở bên ngoài , để ý cái namespace Online.Controllers ( namespace này để phân biệt cái nào của controller nào là của bên ngoài , hay các area )
B2 :Vào App_start -> RouteConfig.cs thêm đoạn code ở trong RegisterRoutes():
namespaces: new string[] { "Online.Controllers" }
B3: Mở file HomeController ở trong Areas của Admin , để ý cái namespace Online.Areas.Admin.Controllers
B4 : Vào Areas -> Admin ->AdminAreaRegistration thêm đoạn code ở trong context.Maproute() :
namespaces: new string[] { "Online.Areas.Admin.Controllers" }
nếu không rõ thì các bạn có thể tham khảo web http://stackoverflow.com/questions/7842293/multiple-types-were-found-that-match-the-controller-named-home
thank you
Sau khi ngồi mò một lúc thì cuối cùng thay cái : namespaces: new string[] { "WebApp.Controllers" } => namespaces: new string[] { "HelloWord.Controllers" } . Đúng là mù tin học thật rồi. Câu này giải thích cho tất các các thắc mắc ở dưới OMG 🙂
cảu e chạy lên thì chỉ hiển thị trang home trong controller ban đầu chứ ko hiển thị được trang home trong arena
của em không có màu cũng không có format gì hết toàn chữ và link thôi…em có làm thiếu chỗ nào không anh…Em không có Admin mà làm thằng vảo View/Shared/Layout.cshtml