I am using Asp.Net MVC 3.0 and I have my _layout.cshtml in Shared folder. Problem is I want to generate menus in this .cshtml which should be loaded from database. But as I understand _layout.cshtml won't have any action etc associated where I can write logic and I don't want to write all this code in cshtml itself. Are there any options to write logic for cshtml within Shared folder?
You can setup a Controller and a view to render the menu and call it inside the _layout.cshtml.
#{ Html.RenderAction("Index", "Menus"); }
Eranga is correct, but let me expand on his answer, to answer your question specifically.
What you can do is create a new controller ("menus" for example), and create an action called default. Have this action return a view, calling it whatever you would like. Now go to your shared folder and add the view using the name you just specified.
Now for the cool part. By default, the MVC framework will look in the controllername/viewname path first, and if it fails it will then look at your shared/viewname path, which is where the view you just created resides! Neat, huh? ;p
Check out http://www.aspnetmvcninja.com/views/view-search-paths for more info on MVC search paths.
#Eranga has given you a good head start on implementing the feature you requested. I think the below two articles will be helpful as well:
Html.RenderAction and Html.Action:
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx
A sample implementation of Html.Action method with caching:
http://www.tugberkugurlu.com/archive/donut-hole-caching-in-asp-net-mvc-by-using-child-actions-and-outputcacheattribute
Related
So I just started at a new company and I'm trying to understand their code and it is quite complex. I am trying to debug a page and I know which view it is and I can set a break point in the view but I can't figure out which controller returned this view. How can I do this?
Recap: I can break in a view and I need to figure out what controller it came from.
If the application is using the Razor view engine (.cshtml files) you can use the following inside your view/layout to display the controller and action names:
#ViewContext.RouteData.Values["controller"].ToString()
#ViewContext.RouteData.Values["action"].ToString()
If it is using the WebForms view engine (.aspx files) you can do something similar with:
<%= RouteData.Values["controller"]%>
<%= RouteData.Values["action"]%>
The controller class will usually be named as in the route data plus "Controller". The action names will usually match a method name in the controller.
You may also consider getting a branch of the project just for you, and then install glimpse via Nuget. That may help you understand better the application.
Views usually are named after Controller action names. And views are placed in folders named after controllers.
Check this one for folder structure: http://www.codeproject.com/Articles/492833/ASP-NET-MVC-4-Part-2-Project-Items
Also you can set breakpoints in controllers and see which one is hit.
And I recommend watching free pluralsight course on MVC. I helped me a lot when I started with MVC.
I'm trying to load an MVC view form a folder outside the location of the app.
My app is in C:\dev\myproject\ and the view file i'm trying to load is located in D:\viewsfolder\something is it possible to do that?
I tried passing an absolute path to the return View() method but that didn't work.
This is not supported by standard ASP.NET MVC. You may take a look at the RazorEngine plugin which allows you to render a Razor view from a string.
You haven't really explained why you need to do that by the way. Maybe there's a better approach to your particular problem.
The best way to do this is to have your Views Precompiled into a dll which can be used across multiple locations with only one code base.
This can be done using the Razor Generator Plugin which you can find here:
http://razorgenerator.codeplex.com/
I am converting an Forms based application to add some MVC 4 View Pages.
I have been able to get pages to work but when I create a view, it never looks in my Views/Shared folder and requires me to put the shared page inside the same view folder instead of defaulting to Shared layout if not specified.
Where is the routing information precedence in MVC4 stored so I configure it to look in the shared folder first? In a slightly related question, is there any way to get the Add Contoller wizard option to show up when right clicking on the Controllers folder and the Wizard to for the scaffolding views? (See picture below of add wizard for example)
Update: After some frustrating time I have added the file into the path desired by the error message and I still getting the error message. It seems the routing is working because when I return a string, the route finds and displays it. The problem appears to happen when I use an ActionResult and for some reason cannot find the "associated" files.
In your _ViewStart.cshtml file in your Views folder, you should have something like:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
That's what tells it where to look for the layout.
For example I have two similar simple MVC applications.
Application1:
HomeController -> Index action which returns AnotherHome view of
Application2:
AnotherHomeController -> AnotherIndex action which also returns AnotherHome view
Application1 Index action:
public ActionResult Index()
{
return View("AnotherHome");
}
I need to run Application1 and render AnotherHome. When I run app it is obvious that I get the following error:
The view 'AnotherHome' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/AnotherHome.aspx
~/Views/Home/AnotherHome.ascx
~/Views/Shared/AnotherHome.aspx
~/Views/Shared/AnotherHome.ascx
~/Views/Home/AnotherHome.cshtml
~/Views/Home/AnotherHome.vbhtml
~/Views/Shared/AnotherHome.cshtml
~/Views/Shared/AnotherHome.vbhtml
How can I force view engine to search view, for example, in Application2/Views/AnotherHome/AnotherIndex.cshtml ???
The short answer is that you can't render a view that's in a different app's application directory. This is a security issue in IIS, and it won't let you access files outside of its application path.
A longer answer is that you could map a virtual directory in IIS into the app directory somewhere. But this would require specific IIS configuration to manage this, and it's something that can easily break if someone else is maintaining things.
It would probably be better to keep separate copies of the view anyways. What happens if you modify it for one app, and then it breaks the other?
You will have to override the ASP.NET default view engine and override the view paths, however you will have to configure somewhere the location of the second application, unless you want to hardcode it. Refer to this question on how to change the view engine search paths, How to change default view location scheme in ASP.NET MVC?.
To share views between apps, compile the views into a dll (Google for razor generator) and share the dll
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.