From 45406bf0826ecb309dd0ffc89fc4391d52d65ed6 Mon Sep 17 00:00:00 2001 From: Praburaj Date: Fri, 29 Aug 2014 13:06:50 -0700 Subject: [PATCH] Moving the admin into a separate area Using areas in the sample --- .../Controllers/StoreManagerController.cs | 3 +- .../Admin}/Views/StoreManager/Create.cshtml | 0 .../Admin}/Views/StoreManager/Details.cshtml | 0 .../Admin}/Views/StoreManager/Edit.cshtml | 0 .../Admin}/Views/StoreManager/Index.cshtml | 0 .../Views/StoreManager/RemoveAlbum.cshtml | 0 .../Areas/Admin/Views/_ViewStart.cshtml | 3 ++ src/MusicStore/MusicStore.kproj | 6 ---- src/MusicStore/Startup.cs | 5 ++++ .../Views/Shared/Components/_ViewStart.cshtml | 4 --- src/MusicStore/Views/Shared/_Layout.cshtml | 2 +- test/E2ETests/SmokeTests.cs | 28 +++++++++---------- 12 files changed, 25 insertions(+), 26 deletions(-) rename src/MusicStore/{ => Areas/Admin}/Controllers/StoreManagerController.cs (98%) rename src/MusicStore/{ => Areas/Admin}/Views/StoreManager/Create.cshtml (100%) rename src/MusicStore/{ => Areas/Admin}/Views/StoreManager/Details.cshtml (100%) rename src/MusicStore/{ => Areas/Admin}/Views/StoreManager/Edit.cshtml (100%) rename src/MusicStore/{ => Areas/Admin}/Views/StoreManager/Index.cshtml (100%) rename src/MusicStore/{ => Areas/Admin}/Views/StoreManager/RemoveAlbum.cshtml (100%) create mode 100644 src/MusicStore/Areas/Admin/Views/_ViewStart.cshtml delete mode 100644 src/MusicStore/Views/Shared/Components/_ViewStart.cshtml diff --git a/src/MusicStore/Controllers/StoreManagerController.cs b/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs similarity index 98% rename from src/MusicStore/Controllers/StoreManagerController.cs rename to src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs index fb1464f47d..e7e4f6cd9e 100644 --- a/src/MusicStore/Controllers/StoreManagerController.cs +++ b/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs @@ -4,8 +4,9 @@ using Microsoft.Data.Entity; using MusicStore.Models; using System.Linq; -namespace MusicStore.Controllers +namespace MusicStore.Areas.Admin.Controllers { + [Area("Admin")] [Authorize("ManageStore", "Allowed")] public class StoreManagerController : Controller { diff --git a/src/MusicStore/Views/StoreManager/Create.cshtml b/src/MusicStore/Areas/Admin/Views/StoreManager/Create.cshtml similarity index 100% rename from src/MusicStore/Views/StoreManager/Create.cshtml rename to src/MusicStore/Areas/Admin/Views/StoreManager/Create.cshtml diff --git a/src/MusicStore/Views/StoreManager/Details.cshtml b/src/MusicStore/Areas/Admin/Views/StoreManager/Details.cshtml similarity index 100% rename from src/MusicStore/Views/StoreManager/Details.cshtml rename to src/MusicStore/Areas/Admin/Views/StoreManager/Details.cshtml diff --git a/src/MusicStore/Views/StoreManager/Edit.cshtml b/src/MusicStore/Areas/Admin/Views/StoreManager/Edit.cshtml similarity index 100% rename from src/MusicStore/Views/StoreManager/Edit.cshtml rename to src/MusicStore/Areas/Admin/Views/StoreManager/Edit.cshtml diff --git a/src/MusicStore/Views/StoreManager/Index.cshtml b/src/MusicStore/Areas/Admin/Views/StoreManager/Index.cshtml similarity index 100% rename from src/MusicStore/Views/StoreManager/Index.cshtml rename to src/MusicStore/Areas/Admin/Views/StoreManager/Index.cshtml diff --git a/src/MusicStore/Views/StoreManager/RemoveAlbum.cshtml b/src/MusicStore/Areas/Admin/Views/StoreManager/RemoveAlbum.cshtml similarity index 100% rename from src/MusicStore/Views/StoreManager/RemoveAlbum.cshtml rename to src/MusicStore/Areas/Admin/Views/StoreManager/RemoveAlbum.cshtml diff --git a/src/MusicStore/Areas/Admin/Views/_ViewStart.cshtml b/src/MusicStore/Areas/Admin/Views/_ViewStart.cshtml new file mode 100644 index 0000000000..ab23e9a239 --- /dev/null +++ b/src/MusicStore/Areas/Admin/Views/_ViewStart.cshtml @@ -0,0 +1,3 @@ +@{ + Layout = "/Views/Shared/_Layout.cshtml"; +} \ No newline at end of file diff --git a/src/MusicStore/MusicStore.kproj b/src/MusicStore/MusicStore.kproj index 6c37376107..a1b5d98213 100644 --- a/src/MusicStore/MusicStore.kproj +++ b/src/MusicStore/MusicStore.kproj @@ -64,11 +64,6 @@ - - - - - @@ -85,7 +80,6 @@ - diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index 56eb81dad3..5f087c1d79 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -86,6 +86,11 @@ namespace MusicStore // Add MVC to the request pipeline app.UseMvc(routes => { + routes.MapRoute( + name: "areaRoute", + template: "{area:exists}/{controller}/{action}", + defaults: new { action = "Index" }); + routes.MapRoute( name: "default", template: "{controller}/{action}/{id?}", diff --git a/src/MusicStore/Views/Shared/Components/_ViewStart.cshtml b/src/MusicStore/Views/Shared/Components/_ViewStart.cshtml deleted file mode 100644 index 8570cc72dd..0000000000 --- a/src/MusicStore/Views/Shared/Components/_ViewStart.cshtml +++ /dev/null @@ -1,4 +0,0 @@ -@{ - //_ViewStart is applicable to ViewComponents as well. Since the layout page renders the ViewComponents in this case adding an override to Layout page to prevent StackOverFlowException. - Layout = null; -} \ No newline at end of file diff --git a/src/MusicStore/Views/Shared/_Layout.cshtml b/src/MusicStore/Views/Shared/_Layout.cshtml index e5f6b093ae..4979bf4ddd 100644 --- a/src/MusicStore/Views/Shared/_Layout.cshtml +++ b/src/MusicStore/Views/Shared/_Layout.cshtml @@ -38,7 +38,7 @@

mvcmusicstore.codeplex.com

- @Html.ActionLink("admin", "Index", "StoreManager") + @Html.ActionLink("admin", "Index", "StoreManager", new { area = "Admin" })
diff --git a/test/E2ETests/SmokeTests.cs b/test/E2ETests/SmokeTests.cs index 2e1015f421..bf0911b2b6 100644 --- a/test/E2ETests/SmokeTests.cs +++ b/test/E2ETests/SmokeTests.cs @@ -225,7 +225,7 @@ namespace E2ETests private void AccessStoreWithoutPermissions(string email = null) { Console.WriteLine("Trying to access StoreManager that needs ManageStore claim with the current user : {0}", email ?? "Anonymous"); - var response = httpClient.GetAsync("StoreManager/").Result; + var response = httpClient.GetAsync("Admin/StoreManager/").Result; ThrowIfResponseStatusNotOk(response); var responseContent = response.Content.ReadAsStringAsync().Result; ValidateLayoutPage(responseContent); @@ -235,7 +235,7 @@ namespace E2ETests if (!RunningOnMono) { //Bug in Mono HttpClient that it does not automatically change the RequestMessage uri in case of a 302. - Assert.Equal(ApplicationBaseUrl + "Account/Login?ReturnUrl=%2FStoreManager%2F", response.RequestMessage.RequestUri.AbsoluteUri); + Assert.Equal(ApplicationBaseUrl + "Account/Login?ReturnUrl=%2FAdmin%2FStoreManager%2F", response.RequestMessage.RequestUri.AbsoluteUri); } Console.WriteLine("Redirected to login page as expected."); @@ -244,10 +244,10 @@ namespace E2ETests private void AccessStoreWithPermissions() { Console.WriteLine("Trying to access the store inventory.."); - var response = httpClient.GetAsync("StoreManager/").Result; + var response = httpClient.GetAsync("Admin/StoreManager/").Result; ThrowIfResponseStatusNotOk(response); var responseContent = response.Content.ReadAsStringAsync().Result; - Assert.Equal(ApplicationBaseUrl + "StoreManager/", response.RequestMessage.RequestUri.AbsoluteUri); + Assert.Equal(ApplicationBaseUrl + "Admin/StoreManager/", response.RequestMessage.RequestUri.AbsoluteUri); Console.WriteLine("Successfully acccessed the store inventory"); } @@ -432,12 +432,12 @@ namespace E2ETests { var albumName = Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 12); Console.WriteLine("Trying to create an album with name '{0}'", albumName); - var response = httpClient.GetAsync("StoreManager/create").Result; + var response = httpClient.GetAsync("Admin/StoreManager/create").Result; ThrowIfResponseStatusNotOk(response); var responseContent = response.Content.ReadAsStringAsync().Result; var formParameters = new List> { - new KeyValuePair("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/StoreManager/create")), + new KeyValuePair("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Admin/StoreManager/create")), new KeyValuePair("GenreId", "1"), new KeyValuePair("ArtistId", "1"), new KeyValuePair("Title", albumName), @@ -446,13 +446,13 @@ namespace E2ETests }; var content = new FormUrlEncodedContent(formParameters.ToArray()); - response = httpClient.PostAsync("StoreManager/create", content).Result; + response = httpClient.PostAsync("Admin/StoreManager/create", content).Result; responseContent = response.Content.ReadAsStringAsync().Result; if (!RunningOnMono) { //Bug in mono Httpclient - RequestMessage not automatically changed on 302 - Assert.Equal(ApplicationBaseUrl + "StoreManager", response.RequestMessage.RequestUri.AbsoluteUri); + Assert.Equal(ApplicationBaseUrl + "Admin/StoreManager", response.RequestMessage.RequestUri.AbsoluteUri); } Assert.Contains(albumName, responseContent); @@ -463,7 +463,7 @@ namespace E2ETests private string FetchAlbumIdFromName(string albumName) { Console.WriteLine("Fetching the album id of '{0}'", albumName); - var response = httpClient.GetAsync(string.Format("StoreManager/GetAlbumIdFromName?albumName={0}", albumName)).Result; + var response = httpClient.GetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", albumName)).Result; ThrowIfResponseStatusNotOk(response); var albumId = response.Content.ReadAsStringAsync().Result; Console.WriteLine("Album id for album '{0}' is '{1}'", albumName, albumId); @@ -473,13 +473,13 @@ namespace E2ETests private void VerifyAlbumDetails(string albumId, string albumName) { Console.WriteLine("Getting details of album with Id '{0}'", albumId); - var response = httpClient.GetAsync(string.Format("StoreManager/Details?id={0}", albumId)).Result; + var response = httpClient.GetAsync(string.Format("Admin/StoreManager/Details?id={0}", albumId)).Result; ThrowIfResponseStatusNotOk(response); var responseContent = response.Content.ReadAsStringAsync().Result; Assert.Contains(albumName, responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("http://myapp/testurl", responseContent, StringComparison.OrdinalIgnoreCase); - Assert.Contains(string.Format("Edit", albumId), responseContent, StringComparison.OrdinalIgnoreCase); - Assert.Contains("Back to List", responseContent, StringComparison.OrdinalIgnoreCase); + Assert.Contains(string.Format("Edit", albumId), responseContent, StringComparison.OrdinalIgnoreCase); + Assert.Contains("Back to List", responseContent, StringComparison.OrdinalIgnoreCase); } private void AddAlbumToCart(string albumId, string albumName) @@ -536,11 +536,11 @@ namespace E2ETests }; var content = new FormUrlEncodedContent(formParameters.ToArray()); - var response = httpClient.PostAsync("StoreManager/RemoveAlbum", content).Result; + var response = httpClient.PostAsync("Admin/StoreManager/RemoveAlbum", content).Result; ThrowIfResponseStatusNotOk(response); Console.WriteLine("Verifying if the album '{0}' is deleted from store", albumName); - response = httpClient.GetAsync(string.Format("StoreManager/GetAlbumIdFromName?albumName={0}", albumName)).Result; + response = httpClient.GetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", albumName)).Result; Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); Console.WriteLine("Album is successfully deleted from the store.", albumName, albumId); }