How do you add .NET MVC controls to umbraco? - c#

How do you add MVC controls to umbraco?
I know in web forms you copy the aspx file into umbraco's usercontrols folder, and the control's dll file into umbraco's bin folder.
But i dont know how to do this for MVC controls.

In solution explorer Go to controller Folder and right click on it then add controller and write the code in it you want

I would recommend reading these to get a handle on how MVC works in Umbraco:
http://shazwazza.com/post/native-mvc-support-in-umbraco-coming-very-soon/
http://umbraco.com/follow-us/blog-archive/2012/10/30/getting-started-with-mvc-in-umbraco-410.aspx

I think you mean one of these two:
1. Child Actions
Read this: http://our.umbraco.org/documentation/reference/Mvc/child-actions
It works in the same way as child actions in Mvc, just that your controllers need to be SurfaceControllers
2. Macro Partials
If what you need is a simple re-usable user controls, then you only need to use partials. You can create a .cshtml file inside Views/MacroPartials then create login to /umbraco and create a Macro that references that partial.
You can the render it with #Umbraco.RenderMacro("myMacro")

Related

Cannot access to physical HTML file

I'm migrate my old fashion asp.net MVC application to an angular application.
I've created a subset of .html files that contains templates that will provide the HTML code to build my angular components.
Problem
I want to put this html files inside the folder "Views" that already exists on my application. But when I try to access to .html files I receive the following message:
But if I put the .html files outside this folder I can access directly to them:
Questions
Can you tell me why this is happening?
There is anyway that I can access to html files inside the folder "Views"?
To make a long story short, the Views folder is a special folder that holds templates used by actions that are routed by ASP.NET MVC. Because of this, you cannot use it to hold files that are meant to be used directly.
I would suggest ditching the .NET Framework all together since you are going Angular.
If that is not an option or you would like to retain ASP.NET MVC functionality, then simply use another folder (like you have already done by moving the Templates folder to the root).
Try adding an explicit ignore in your RouteConfig
routes.IgnoreRoute("{file}.html");
That should prevent the default routing from taking effect
To make this work, I suggest you take the html markup in your html file and put it on a .cshtml view, that you will render using a GET action in the home controller for instance (as you would normaly do in the MVC pattern).
The other way to achieve this (which I don't recommend), is to add your html file to a deployable folder (like the js folder for javascript files), and than you can call your file using a direct link, however you will lose any capabilities of the MVC pattern this way.
Hope this helps.
As html files are working in views folder, Can you check the path i.e 'Editor/V2/Templates' after 'Views' folder whether it is correct?

Share PartialView between projects in a solution?

I am developing 2 MVC5 websites. They run seperately, but actually related to each other (1 Admin site, and 1 User site). In both site, I have to render a graphical board using HTML table. I am using PartialView with a Model to render it.
Currently, this is my solution structure:
Project.Common (A class library project, contain the model)
Project.Admin (MVC5 project for the Admin site), refering to Project.Common.
Project.User (MVC5 project for User site), refering to Project.Common.
To render the graphical board, I will use BoardData class from Project.Common, which is ok. But with current solution structure, I have to create 2 PartialView in each MVC5 project, which may be hard to maintain later.
Please suggest a good solution for reusing the PartialView. I already know I can write Helper, but that way, I have to write HTML code inside C# code, which is not very good. Is there any way to do this within Razor View?
One of the options you can use is add a link to a file. In this case keep your Partial view in just single project and in second project use Add -> Existing Item, select your partial view from the first project and click on a small down arrow next to the Add button and choose Add as Link. More info can be found on MSDN, see section Add an existing item as a link.
In this case you will have a partial view only in one project and any changes to it will automatically be applied to other project as well.
You should embed your views into the Project.Common dll, a detailed description can be found on https://visualstudiogallery.msdn.microsoft.com/f28290ce-d987-4f91-b034-707031e10ce6

Load views from location outside the mvc website

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/

How to specify different AddController code templates for different MVC 3 Areas?

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.

Organizing MVC2 Views with a subdirectory

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.

Categories

Resources