Calling Partial View from different projects - asp.net-mvc

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.

Related

How to change MVC3+ Views after application has been deployed (without redeploying)

As I understand one of the benefits of the MVC pattern is being able to substitute other Views without having to change the Model or Controller.
Since the Razor view engine compiles the views, how can I change or tweak the views after they have been deployed without having to re-deploy the application?
Razor views are compiled dynamically at runtime when they are requested by the ASP.NET runtime. You could replace your .cshtml/.vbhtml files directly on your webserver and the changes will be automatically picked up.

Razor Generator Build Action

I am giving:
RazorGenerator
a try. The quick start guide mentions to switch the build action to 'None'. However, this means that the views are not published. Is the build action 'Content' correct as this allows publishing?
Thanks.
The purpose behind Razor Generator is that it pre-compiles your razor views, translating the markup in your cshtml files into C# code that gets executed when the view is rendered.
This can happen at design time, when you save a view, if you set the custom tool property for that view to RazorGenerator. Alternatively, it can happen at build time by integrating the Razor Generator MSBuild target.
As you mentioned, without Razor Generator you normally set the build action for your views to "Content". The markup is parsed at runtime, when the view is first requested, and a compiled view is made available in a dynamically generated assembly. If you use Razor Generator, there is no need to copy the markup around because the compiled views are already part of your web assembly. This is why you can set the build action to "None" on your views.
Other Details:
Razor Generator extends ASP.NET MVC by adding its own PrecompiledMvcEngine to the collection of ViewEngines. This is used to locate the compiled views as they're requested.
There are some properties of the PrecompiledMvcEngine that, if set, will have the engine check if the view exists on disk and use it if it is newer than the pre-compiled view it has in the assembly. This can be useful at design time, so that you can see changes made to your views without rebuilding everything.
As I understand it the RazorGenerator creates a Html helper that you can use in your views. The HtmlHelper is compiled as a class with an extension method. The view that it is based on should not be published since you're not supposed to use it directly within your project. Thus the view should have build action set to none, exactly as stated in the quick start.
Step 3 in the quick start illustrates how you use the created Html helper:
The nested file will be compiled with your project and can be
referenced as a regular helper. e.g. Html.WriteSpan("Hello world")

What is the convention for storing files containing action filter classes?

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.

ASP.NET MVC 2.0: Shared View Controller

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).

Render View (or Partial) In another project?

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

Resources