I have a pretty basic question, but I cannot find the answer online without resorting to asking it myself. In an ASP.NET MVC 2 WebSite, if I have a Shared view located in a Shared folder. Let's say the view is called Error, where is the controller for this View? Do I create a SharedController?
You don't create a SharedController. In any controller you want the view available, either create a Method called Error or have a Method return View("Error").
Basically if MVC can't find the View in the Controller Named Directory it looks in the Shared folder for it (which also works for Partial Views and Controls).
Related
I've watched a lot of video tutorials and read even more articles. I don't think I've seen one that answers this question and I'm stuck. :S
I created an MVC4 Application with Web API. Going off of the tutorials I've read I went with a standard naming convention. Say for example, /api/user and /api/user/id, etc.
Now I would like to create CRUD pages for the User entity. Of course, these pages would fit well under the /Admin/ folder, but I can't create a controller that generates views of /Admin/User/Create, /Admin/User/View, etc., since this would require creating a controller called UserController - but this is already created for the WebAPI.
Doesn't it seem standard or at least 'ok' to create CRUD pages for my Entities with this folder structure:
Views/Admin/User/
Views/Admin/Product/
etc, etc.
This allows each entity/table to have its own folder with its own Create.cshtml, Index.cshtml, etc.
But again, I have already created a WebAPI with this structure:
api/User/
api/Product/
So, now I want to create some Admin pages to manage the database. When I create a MVC controller with read/write using Entity Framework I can't figure out what to name the controller to give me the URI structure I'm wanting.
Of course, I don't really care what the controllers are called, but I don't know what steps are required to get the URI structure. For example, I created an MVC controller and named it AdminProduct, but now I have to go to /AdminProduct/ to see those pages.
Can someone point me?
You can have two controllers with the same name as long as they are in different namespaces. For example, I add all of my System.Web.Http.ApiController classes under the /Controllers/Api folder in my project. By default, Visual Studio will include these controllers in a namespace called <ProjectName>.Controllers.Api. Then, inside the /Controllers folder you can add System.Web.Mvc.Controller classes there, and they will be in the <ProjectName>.Controllers namespace. So, long-story-short, you can have an MVC controller named UserController and a WebApi controller named UserController, and there is no conflict.
The views for your MVC controller actions, like /User/Create and /User/Delete should be put under the /Views/User/ folder of your project - each with the same name as the action in the UserController. As far as having admin pages, why not create an AdminController or using the [Authorize] action filter on those actions you want secured?
I'm not sure I understood your entire problem. ASP.NET MVC binds controller names and actions to views by this convention.
Within the project I'm using this code #Html.Partial("../ReferenceChangeLog/ReferenceChangeLogPopUp") for calling the controller and action.
Here ReferenceChangeLog is Controller Name. ReferenceChangeLogPopUp(ReferenceChangeLogPopUp.cshtml) is Partial View Name.
How can I set the path for calling another project's controller and action ?
That's not supported out of the box. By default Razor views are only resolved within the current project which is what you deploy as an ASP.NET application in IIS. If you need to implement this, you will have to write a custom Razor view engine capable of retrieving views from arbitrary locations on the file system. Here's, for example, an article which explains how you could embed Razor views into assemblies and reuse them across multiple projects.
In tutorials and walkthroughs on developing an MVC3 (MVC2) using EF (EntityFramework, Entity Framework 4.1/4.2) I observe quite different orders of adding Model, View, Controller to a project in a Microsoft Visual Studio 2010.
What are cons and pros of different orders of adding M, V and Cs?
and of, for example, adding a model before and, more specifically, after a view and controller?
There is no specific rules for adding one over the other first. When you create an empty ASP.NET MVC3 Project, It will come with some default folder structure which includes one Controller folder, Views folder and Models folder.
Now If you are beginner, This is what i suggest. Add a controller first.
Simply right click ont he Controller folder and Select Add->Controller from the context menu and add your first controller(Give the name as HomeController). It will come with a Default Index action method and you can see a return View statement. Run your project now. It will show you an error saying that it can not find a view. So now it is the time to add a view. Go the index action in home controller. Right click on the Return View() statement and select Add View that will add a view (index.xshtml) under the home folder under Views. Now run the app and you will see the page content.
If you are going to interact with your database, you can add model classes. If you can add POCO class file to your Models folder or you can have it on a different library which is refered to this projcet. It's all up to you.
As Lavinski mentioned, If you create your models first, You can use Scaffolding to create the controller actions for you. But If you are beginner, I would suggest you to create your controllers and views by hand. That will help you to understand MVC configuration works
I have some action filters that I want to apply to more than one controller. I looked around but can see no MVC conventions on where to place these files. The only place I was able to see them used was in NerdDinner and in that case all I could find was an onactionexecuting method that was inside of a controller.
For those of you who use Action Filters. Where do you place these in the MVC file structure?
They go into a folder in the same level as the Models Views and Controller level, called ActionFilters.
I've also seen them go into a subfolder under Controllers, since they normally apply to actions on controllers.
I place all ActionFilter classes in a folder simply called Filters in the MVC project.
However, my team recently created a Web.Mvc.Common project, and that's where we put the Filters folder now, so that they can easily be reused by other projects.
i have a solution with the following two projects - MyNamespace.Services and MyNamespace.Web.
Web contains a MVC web application.
In the Service project i have a EmailService class that takes care of sending out emails to the user.
I want to use either a partial or a view (ascx or aspx) for email templates.
I have found several solutions on how to render a partial view and get the result as a string which works fine if the template is inside the web project (as it is a controller in the web project that calls the email service).
(the 2 methods i am trying to use is either http://developersisland.blogspot.com/2009/01/renderpartial-to-string-in-aspnet-mvc.html (at the bottom of the blog) or http://www.brightmix.com/blog/how-to-renderpartial-to-string-in-asp-net-mvc/)
But my email templates are located in the Services project.
How can i refference the path to the templates (partial/view) in my Service project from inside the Web project, that works with either LoadControl or RenderPartial which both takes a virtual path as a parameter ?
It seems like no matter what i do the root directory is set to the Web projects directory.
Is it possible ?
Would be nice to be able to make it work independently of the web project somehow.
I don't think this is possible without developing your own view engine. The default view engine will only look in certain locations for the partial view -- which includes the current view folder and the shared views folder. I don't think you can search for views outside the current project since those views aren't registered with the view engine.
You can consider just creating your HTML helpers to render emails and return it as a string.
Doesn't really matter whether it is partial view or a method returning a string with HTML. i actually think that for your case helper methods would be a better choice.
A simple helper method is also more flexible in the ways you can use it.
You could try creating a custom view engine locator or virtual path provider. Here are a few examples that may help you get going:
Views in seperate assemblies in ASP.NET MVC
Grouping Controllers with ASP.NET MVC
How to use virtual path providers to dynamically load and compile content from virtual paths in ASP.NET 2.0
All of the links above are good, this might help as well. you will certainly be able to get it to find and use the views. The problem I had was in working with them, there was no code completion etc in the other projects. It was semi possible to get that as well by fiddling around with the project file but to be honest I ended up going with the Grouping solution above
Plug in architecture for ASP.NET MVC