I have for some time created a few mvc websites. The other day my super suggested I put them all into one solution as they - more or less - is related to each other.
So I have come a long way with making the projects work more or less on their own.
Yesterday I hit a little bump where I am to put one project where you log in in order to register data. I have moved the login code to the first website (front?) and now that works. It was when I wanted to create the link (ActionLink) to link to the registration project, I started doing research on moving/transforming/changing a project into an area in mvc5.
So my question:
Has anyone moved/transformed/changed a mvc5 (or 4 or 3) project into a mvc5 area?
Is there something to worry about when doing this?
There are several things to consider when merging a project.
Routes and namespacing - If you controllers that have the same names in multiple areas, you will need to add a namespace entry to the route entries. Details here http://haacked.com/archive/2010/01/12/ambiguous-controller-names.aspx/
Make sure you have an area registration file that sets up each area. i usually add 1 area, then copy it's registration file and rename appropriately
Script and css dependencies - make sure that you get all those added to the root, seperate them in area folders if you like, then add them in at the correct area by using a section in your layout for dependencies
Adding a hyperlink between areas now needs to include the area name if you have conflicting controllers , you can check these answers How to specify an area name in an action link?
Think that about covers most things I have found when doing areas
Si
Related
I've been developing quite a complex web app for the company I am working for, and I have a problem with routing in asp.net mvc.
I know about the areas, but I don't want to use this approach.
Url structure I have now:
Default
Administration_Users/*
Administration_UserGroups/*
Configuration_Report/*
Configuration_Schematics/*
Dictionaries_DeviceCatalogue/*
Dictionaries_DeviceParameters/*
... and so on. (* means multiple methods inside, like Index, Edit, etc.)
Url structure I want:
Default
Administration/Users/*
Administration/UserGroups/*
Configuration/Report/*
Configuration/Schematics/*
Dictionaries/DeviceCatalogue/*
Dictionaries/DeviceParameters/*
I CAN use areas, but the problem is that these will later expand, as it is just one part of the web app. So, they will look like more this:
EquipmentManager/Configuration/Report/*
EquipmentManager/Configuration/Schematics/*
EquipmentManager/Dictionaries/DeviceCatalogue/*
EquipmentManager/Dictionaries/DeviceParameters/*
So, my question is: Is there a working namespace approach instead of area one?
Basically, I want InternalWebApp.Controllers.EquipmentManager.Configuration.Report controller that is located in corresponding folder map to views which are located in InternalWebApp/Views/EquipmentManager/Configuration/Report folder of my solution without having to map every controller manually in RouteConfig.cs.
I have use two Project under single solution --
PayrollMVC is Name of Solution which contains two Project
1) Payroll
2) Employee
when i Login from Payroll and Redirect to Employee Project Controller action than it does not Call Controller Action and Layout of Employee Project ..Please
Suggest me How to Map Two project and how to Call Action from one Project to Another and
also I Please Tell me What is the issue of not Calling Action and Layout of Employee Project
One solution to call an action from one solution to the other would be to use Server Transfer in the controller like so:
public ActionResult Whatever()
{
string url = //...
Request.RequestContext.HttpContext.Server.TransferRequest(url);
return Content("success");//Doesn't actually get returned
}
Copied from https://stackoverflow.com/a/20913290/201648
This is useful if you want to quickly transfer a handful of controllers to a page on another website using the same URL, e.g. http://example.com/payroll/foo could show the content http://example.com/employee/foo but still appear at http://example.com/payroll/foo. This solution is quick to implement but also hacky, and it doesn't solve you layout issues (please continue reading about areas below for a complete solution).
In terms of not calling the actions and layout, it's because they're in a different project (so the current project doesn't know about them). For this reason you'd probably want to go to the trouble of setting up areas. There is a full guide for areas here:
https://msdn.microsoft.com/en-us/library/ee671793%28v=vs.100%29.aspx
You can setup shared layouts for areas as described in this link:
http://brockallen.com/2012/08/31/sharing-a-single-_viewstart-across-areas-in-asp-net-mvc/
All the routes (for the controllers) can be configured centrally. I'd start by looking at Registering Area Routes and Linking Between Areas from the first link I posted.
I know how to customize a controller/view template for an MVC project. However, I have a few different MVC 3 Areas in my project and each Area has its own Models, Views and Controllers. Is there a way to specify different code templates for each Area? I tried to place a CodeTemplates folder in one of my Area sub folders but it didn't work.
The idea is, when I right click on any Controller folder in an Area and select Add -> Controller, I want it to use that Area controller template.
UPDATE:
I will be happy with being able to select the controller template I want to use, but still wondering if it is possible to specify CodeTemplates for the different areas.
Looks like MVC Scaffolding might be my only option here. I will show specific solution when I get it working.
Rereading you question, I realized my answer may not be what you are trying to do. Do you wan't the auto generated code in the view to use a different template? Or did you wan't the page to look differently, like with css changes, etc? My answer applies to the latter. If you want to use different code templates, it seems like (according to this) as long as you give them different names you should be able to select the one you wan't to use. You could also probably just remove the ones you don't want from the project then add them back later.
Create 2 files in the folder you want the template to be applied to, one named _ViewStart.cshtml and the other _Layout.cshtml
In the _ViewStart file, add this:
#{
Layout = "~/Views/Reports/_Layout.cshtml";
}
This will point to the _Layout so change the path as needed. In the _Layout file add whatever you want, just like the layout in the Shared directory. And for any other folder you want to use this layout, just add the _ViewStart file to that directory and point it to the _Layout file you want.
We have a fairly large MVC project with a large number of views and partial views (display/editor templates). Pretty much 95% of our UI is in our templates, our aspx views are just placeholders that call our ascx templates. As such, whenever we open that Views folder in VS2010, it's fairly painful to scroll down to the Shared folder for us to manage our templates.
We'd LIKE to change this hierarchy just a bit. Instead of having a folder for each controller under Views, instead we'd like to have a folder called Pages under Views and have our per-controller folders under here. This way, when we open Views we have 2 folders: Pages and Shared.
Is this easily possible without going back and editing every single one of our controllers where they simply return View(); to return View("Pages/MyControllerName/MyActionHandler");? I'd like the controllers to still simply return View(); and without my URLs changing. So in other words, I just want to move my root directory for my Views into Views/Pages, but not for templates - they stay as they are.
Solution we went with:
global.asax.cs: (add these as the first 2 lines of Application_Start() - change nothing else)
var locations = ((WebFormViewEngine)ViewEngines.Engines.First()).ViewLocationFormats.ToList().Select(x => x.ToLower().Replace("views/{", "Views/Pages/{"));
((WebFormViewEngine) ViewEngines.Engines.First()).ViewLocationFormats = locations.ToArray();
This allows our Views folder to have 3 things in it:
Pages
Shared
web.config
Since we do 95% of our UI work within our Shared DisplayTemplates and EditorTemplates, this rmeoves the mostly-ignorable stuff from being in-our-face all the time as developers. While the suggested answer was to do ViewEngines.Engines.Clear(); and then add a newly-instantiated one, we had problems with MVC playing nice with the new one. So we chose to instead alter the functional and already-instantiated instance. I don't like the casting, and we'll put some code around that to be better, but this is the functional code to get anybody started that also wants to do this.
You need to register a ViewEngine with the ViewLocationFormats property set to include your new locations.
I have searched on Google (may be with wrong keyword) and visited the tutorials of ASP.NET/MVC website. But didn't understand the routing of MVC properly.
I have a scenario of the following figure. I want to display the Index.cshtml when the website lunched.
I have changed the RegisterRoutes method of Global.asax.cs file in various ways. The latest code (not working) is below
routes.MapRoute(
"App", // Route name
"App/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I will be very happy if someone explains the MapRoute method with different example along with answering my problem.
I don't think the problem is with your routes, I believe it is caused by moving your View and Controller folders to the App folder.
MVC use "convention over configuration" for stuff like where the folders are located, I'm guessing it can't find the Views/Controllers in the new folders?
From here:
These folders are included even in an
Empty ASP.NET MVC application because
the ASP.NET MVC framework by default
uses a “convention over configuration”
approach and makes some default
assumptions based on folder naming
conventions. For instance, controllers
look for views in the Views folder by
default without you having to
explicitly specify this in your code.
Sticking with the default conventions
reduces the amount of code you need to
write, and can also make it easier for
other developers to understand your
project.
I'm not saying that it's not possible to have a folder structure like you have, I just dont believe its supported out of the box.
Personally I'd recommend moving back to the default, if you need to keep this structure then I think you may need to implement your own ControllerFactory and ViewEngine (I'm not sure if these are the correct hooks that you would need to change).
Edit:
Are you trying to implement App as an Area?
Edit2:
If you are trying to add App as an area there are some steps you will need to follow, The reason I wasn't sure if this was what you were trying to do is because I thought areas had to exist within an Areas folder.
There are some steps outlines here Including how to register your area if that is what you are trying to do.
ASP.NET MVC 3 has a built in feature for sub folders. That is called Areas (thanks #Morten for let me know the word). But after finishing the MSDN Walkthrough for Areas, I was still unable to run the web application. The error was showing Configuration errors. #Danny also noticed that.
Then I searched and found that, the Web.config files in the sub folders shouldn't contain application specific properties.So, I removed those properties from the Web.config files. Then I was able to run. But there were some runtime errors when I wanted to navigate to my sub folder/area views. The error was "Could not load type ‘System.Web.Mvc.ViewPage<..<>’" when I click on the links for the views of the Areas folder.
Then I again searched and found helpful this post. And then my application successfully run and I can navigate all of the pages.