Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 389
MartinF Avatar asked Apr 25 '09 23:04

MartinF


People also ask

What is difference between partial and render partial view?

The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.

When should partial views be used?

Partial views are an effective way to: Break up large markup files into smaller components. In a large, complex markup file composed of several logical pieces, there's an advantage to working with each piece isolated into a partial view.

What is a view that renders a portion of view content called?

Partial view in ASP.NET MVC is special view which renders a portion of view content. It is just like a user control of a web form application. Partial can be reusable in multiple views. It helps us to reduce code duplication.


1 Answers

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.

like image 64
tvanfosson Avatar answered Oct 03 '22 12:10

tvanfosson