posts - 81, comments - 213, trackbacks - 0

Allowing for Dynamic Embedded View Substitution with MVC


A while back I found an interesting post on embedding resources in MVC at "The Glass is Too Big" (http://www.wynia.org/wordpress/2008/12/05/aspnet-mvc-plugins/). Following the method was straightforward and allowed views to be embedded within a Component Class Library. Well, two extra items would be nice. It would be nice to not have to write that large plug-in path, and if a view was defined in the views folder it would be returned in lieu of the embedded view.

Adding this support required two modifications. First the AssemblyResourceProvider was modified to check for the view's existence by adding a Regex pattern match and checking for the file. If the file is found within the view folder, the file is returned, if not the embedded resource will return.

 

   58             // Check to see if a file has been added to the views folder.  If so, return that view.

   59             Match m = Regex.Match(path, @"~/Plugin/[\w\.]+.dll/([\w\.]+).Views.([\w\.]+).aspx");

   60             if (m.Success)

   61             {

   62                 string assemblyMatch = m.Groups[1].Value;

   63                 string viewPath = m.Groups[2].Value;

   64                 string physicalPath = HttpContext.Current.Server.MapPath(String.Format("~/Views/{0}/{1}.aspx",

   65                     assemblyMatch, viewPath.Replace(".", "/")));

   66                 if (File.Exists(physicalPath))

   67                     return File.Open(physicalPath,FileMode.Open);

   68             }

http://www.codeplex.com/unifico/SourceControl/changeset/view/1491#9855

 

To avoid having to write out the plug-in strings I wrote an extension method to write it and return the ViewResult. Reflection is used to gather the assembly name.

 

    9     public static class ControllerPluginPathExtender

   10     {

   11         public static ViewResult PluginView(this Controller controller)

   12         {

   13             string controllerName = controller.RouteData.GetRequiredString("controller");

   14             string viewName = controller.RouteData.GetRequiredString("action");

   15             string assemblyName = controller.GetType().Assembly.FullName.Split(',')[0];

   16             string pluginPath = String.Format(

   17                     @"~/Plugin/{0}.dll/{0}.Views.{1}.{2}.aspx",

   18                     assemblyName,controllerName,viewName

   19                     );

   20             return new ViewResult{

   21                 ViewName = pluginPath

   22             };

   23         }

   24     }

http://www.codeplex.com/unifico/SourceControl/changeset/view/1491#52892

Now using the embedded option is less tedious, for example:

 

   33 

   34         [Authorize(Roles="Admin")]

   35         public ActionResult Index()

   36         {

   37             ViewData["Title"] = "Account Admin Home";

   38             ViewData["Message"] = "Welcome to the account admin";

   39 

   40             return this.PluginView();

   41             //return View("~/Plugin/App.Account.dll/App.Account.Views.Admin.Index.aspx");

   42         }

 

It would be nice to not have to use 'this', but I can't see a way around it without rebuilding MVC, not something I want to start doing. Also some caching and less reflection might be nice.



Update: The VewData and TempData have to be added to pass the models to the view

   11         public static ViewResult PluginView(this Controller controller)

   12         {

   13             string controllerName = controller.RouteData.GetRequiredString("controller");

   14             string viewName = controller.RouteData.GetRequiredString("action");

   15             string assemblyName = controller.GetType().Assembly.FullName.Split(',')[0];

   16             string pluginPath = String.Format(

   17                     @"~/Plugin/{0}.dll/{0}.Views.{1}.{2}.aspx",

   18                     assemblyName,controllerName,viewName

   19                     );

   20             return new ViewResult{

   21                 ViewName = pluginPath,

   22                 ViewData = controller.ViewData,

   23                 TempData = controller.TempData

   24             };

   25         }


Print | posted on Friday, January 2, 2009 10:18 PM | Filed Under [ Web Programming ]

Feedback

Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Hello, thank you for the enhancements. I am using this code, but my embedded view can't seem to see Model and errors when it tries to load the type that my view inherits from. Can you post or point me towards a sample view that uses <%= Model.FieldName %> in it? Thank you, Jeremy
11/2/2009 10:54 AM | Jeremy Biros
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Did you ever solve the problem of loading models for the embedded views? I'm running into the exact same issue. Thanks!
4/14/2010 11:55 AM | Sara McCormick
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Now I know it and will tell to my friends about this blog
4/27/2012 10:42 PM | Pure Berry Max
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Hey this was a great post. I really liked what you had to say
4/29/2012 8:02 AM | Xenadrine
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

I am definitely enjoying this post and regularly checking for new posts. These posts are really good and definitely beneficial. Great stuff from this part of the internet. This is the best weblog for anyone who wants to know about this subject.
5/6/2012 9:50 PM | pradaxa attorney
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Terrific way of expressing those things in your post. Clear cut meanings. great
5/19/2012 11:44 PM | Lipozene
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Wonderful posts you have on your blog and I have bookmarked you and will visit yours often.
6/11/2012 10:04 PM | Lipozene Reviews
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

You rock it up and keep rocking.
6/13/2012 12:12 PM | Lipozene
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

This is beyond doubt a blog significant to follow. You’ve dig up a great deal to say about this topic, and so much awareness. I believe that you recognize how to construct people pay attention to what you have to pronounce, particularly with a concern that’s so vital. I am pleased to suggest this blog. :) nice..
6/24/2012 1:23 AM | Lipoorexall
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Hey that was great to read. Thanks for the great post .Loved every part of it.
8/2/2012 11:00 AM | Lipozene Reviews
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

This is beyond doubt a blog significant to follow. You’ve dig up a great deal to say about this topic, and so much awareness. I believe that you recognize how to construct people pay attention to what you have to pronounce, particularly with a concern that’s so vital. I am pleased to suggest this blog.
8/3/2012 5:51 AM | Hydroxycut
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon!
8/4/2012 9:10 AM | Lipozene
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

I am very much overwhelmed by your thoughts for this particular story. A more deeper and staged knowledge would be good for me
8/12/2012 3:21 AM | WeightLoss
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon! thanks....
8/13/2012 2:34 AM | Fast Weight Loss
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

You got a definitely helpful blog I’ve been right here reading for about an hour. I’m a new and your accomplishment is quite a lot an inspiration for me :)
9/2/2012 4:54 AM | Click here
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

v
9/11/2012 12:03 AM | Lipozene
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Hey that was great to read. Thanks for the great post .Loved every part of it :))
9/11/2012 12:05 AM | Lipozene
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Came across your blog when I was searching bing I have found a lot of info that I found to be quite useful.
9/15/2012 9:03 AM | Details
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Your website is very the most interesting. I loved your website a lot. Thank you.
9/22/2012 8:41 AM | Raspberry Ketone
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

I have been meaning to write something like this on my website and you have given me an idea. thanks:)
9/24/2012 10:51 AM | Green Coffee Bean Extract
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon!thanks:)
9/27/2012 8:53 AM | Lipozene
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

This is what I have been searching in many websites and I finally found it here. Amazing article. I am so impressed. Could never think of such a thing is possible with it...I think you have a great knowledge especially while dealings with such subjects. thanks:):)
10/22/2012 8:39 AM | Lipozene Reviews
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Thanks so much with this fantastic new web site. I’m very fired up to show it to anyone. It makes me so satisfied your vast understanding and wisdom have a new channel for trying into the world.
10/25/2012 10:31 AM | Lipozene
Gravatar

# ugg saappaat

Uggs boots keep feet toasty even http://www.uggssaappaatsuomi.org/ in the freezing cold days and ugg saappaat therefore have become the ugg saappaat suomesta first choices of many customers xiaocaicl15. ugg saappaat boots This kind of footwear looks fashionable ugg saappaat oulu and unique and is available in different uggs saarbrücken styles and colors for your choices. Made http://www.uggstovlerdanmark-s.com/ with twin-faced sheepskin, these boots feel incredible ugg boots udsalg warm and comfortable. Many celebrities are ugg boots dk also spotted wearing them during the winter season ugg boots tilbud to go through the cold winter. But this conception is ugg boots online quite wrong, as wearing them sensibly especially with jeans and feathery coats really looks cool and exquisite.
31
11/13/2012 3:15 AM | Zkjykgsng01
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Outstanding blog post, I have marked your site so ideally I’ll see much more on this subject in the foreseeable future.
11/26/2012 5:08 AM | School and learning
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Your bot-sense image doesn't work and so your site has been spammed by the fake bot brigade at fake advertising urls dot com! Congratulations... and I'd suggest turning on moderation! LOL
8/18/2013 11:48 AM | Fake Bot Brigade

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 6 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET