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

30
47



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/

30 COMMENTS

  1. 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.

  2. 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

  3. 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

  4. Trong dòng code : new { action = "Index", controllers="Home", id = UrlParameter.Optional } thì viết hoa Controller vẫn chạy được.

  5. 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

  6. 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 ạ?

  7. 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

  8. 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!!!!!!!!!!!!

  9. 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.

  10. 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 ạ ?

  11. 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

  12. 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 🙂

  13. 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

  14. 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

LEAVE A REPLY

Please enter your comment!
Please enter your name here