posts - 64, comments - 387, trackbacks - 4

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 02, 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

The specification must define a technique for registering EventListeners in groups. These groups will then have specified behavior in which attempts to modify the flow of an event will be restricted and affected only the group to which the EventListener in question belongs.It is also required that whatever technique is specified to accomplish this purpose be compatible with the existing DOM Level 2 Event model and any EventListeners registered using DOM Level 2 Event model methods.
4/24/2010 12:38 AM | strategie des jeux de casino
Gravatar

# London Pearson

Wonderful post! I like your blog, and am a regular follower. I will be back  monday!
10/26/2010 2:36 AM | London Pearson
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

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
12/7/2010 2:50 PM | forklift training certification
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Thanks very good sharing
2/16/2011 10:46 AM | ssk hizmet dökümü
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Real good articles here mate, I will be back again as im learning so much about this area that im interested in a lot. Thanks for the great insight you’re a gem buddy
12/14/2011 1:27 AM | Glasfiberflagstænger
Gravatar

# fractional laser Adelaide

It is easy to see that you are impassioned about your writing. I wish I had got your ability to write. I look forward to more updates and will be returning.
12/16/2011 5:17 AM | fractional laser Adelaide
Gravatar

# Productive Balance | Get More of the RIGHT Things Done



I am really enjoying reading your well written articles. I think your blog posts looks unique and informative.I recommend everybody to read this post...thanks.


12/17/2011 4:41 AM | Life Balance
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Do you acknowledge that it's correct time to get the business loans, which would make you dreams real.
12/19/2011 6:09 PM | Henson22Bertha
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

There is obviously a lot to know about this.I think you made some good points in Features also.
how to get rid of sugar ants
how to get rid of dark circles naturally
12/20/2011 5:38 AM | avena bailey
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

So helping code for me.
12/20/2011 6:03 AM | boiler installation london
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Buy facebook likes from www.fanbullet.com" title="www.fanbullet.com">www.fanbullet.com will be an ultimate source for the successful online marketing. I decided to buy facebook fans and likes for targeted user. www.fanbullet.com" title="www.fanbullet.com">www.fanbullet.com has provided a new marketing strategy for my business promotion. Really, it is quite appreciative source to promote business online.
12/21/2011 7:00 AM | Buy facebook fans
Gravatar

# Web Design London

if a view was defined in the views folder it would be returned in lieu of the embedded view.
12/22/2011 12:54 PM | Web Design London
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

I argue that rss feed submissions gives the most perfect and most apposite traffic to your site and rss directory submission service will aid to do that.
12/24/2011 12:14 PM | rss-feeds-submission.com
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Very awesome sharing. I read your blog. It is really a good information which you share on this blog. Keep it up.
12/26/2011 11:55 AM | Nashville Weight Loss
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

The University Writing services will be able to provide very good papers. College students generally utilize such services to order different types of research papers.
12/27/2011 5:35 AM | Writing an Essay for Scholarship
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

I know that some thesis writing services or dissertation writing service would employ you to write such of great mba thesis more or less about this topic.
12/28/2011 1:58 AM | dissertation
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

You are so inspirational and you talk sense. That’s very important. You’re educated and you have a lot of heart. I like your content! Thank you!
12/30/2011 9:01 AM | norwalk boot camp
Gravatar

# Glasgow Web Design

I wrote an extension method to write it and return the ViewResult. Reflection is used to gather the assembly name.

1/1/2012 12:59 PM | Glasgow Web Design
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

i feel from a long time that this this topic is quite tough for me and i cant understand but after reading this i fell happy and understand all material very well now i am happy and want to say you thanks.... bilpleje
1/2/2012 9:26 AM | bilpleje
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

With your help only students have some knowledge about custom dissertation! So they order the professional dissertation thesis connecting with this good topic from the buy thesis service.
1/6/2012 8:04 PM | buy dissertation
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

That should be the really good favor for the dissertation service to utilize your brilliant topic about this good topic for the thesis report creating. Therefore, all the scholars will get a chance to buy the premium quality thesis research.
1/6/2012 8:09 PM | thesis writing
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

I love this help and advice and find this is a superb learning resource post and your information material is highly insightful. We have added this page and will disclose it with my fans.
1/9/2012 7:51 AM | condos in hyde park
Gravatar

# Lic

[url=http://www.feewatches.net/]replica watches[/url]
replica watches
replica watches
1/10/2012 3:56 AM | cheap wedding dresses
Gravatar

# Garland Tilus

ストッキング 男物 着物 楽天 ベビードール コスプレ カラオケ 銀座 着物 レンタル 襦袢 ベビードール 服 けいおん 制服 品川女子 制服 ゴスロリ浴衣
1/10/2012 3:59 AM | 留袖 髪型
Gravatar

# Stephen Bunker

振袖 ゴスロリ処刑人 着物 髪型 ロング 初音ミク コスプレ衣装 ベビードール 服 大人 馬車道 制服 振袖 販売 制服 リボン バニーガール 浴衣
1/10/2012 7:19 AM | 着物 髪型 ロング
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

I desired to thank you for this exceptional read!! I absolutely enjoyed every single small little bit of it. I’ve you bookmarked your internet site to look at out the new stuff you post.
1/12/2012 2:26 AM | tantric massage london
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

I never imagined how much stuff there was online about this! Thank you for making it easy to grasp
1/12/2012 2:27 AM | massage london tantric
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Interesting articles are published here. By reading it I acquired great deal of knowledge on various subject. Thank you for sharing with us.
1/12/2012 2:28 AM | massage erotic london
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

Great information. I got lucky and found your site from a random Google search. Fortunately for me, this topic just happens to be something that I've been trying to find more info on for research purpose. Keep us the great and thanks a lot.
1/14/2012 7:42 AM | Boliger i Thailand
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

This is very important to compose the correctly performed Shakespeare essay and free essays to reach the best mark at the high school.
1/19/2012 4:55 PM | europe essays paper
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

I want to say thanks the forum proifles services for great quality of forum posting options! That aided my site very much!
1/20/2012 12:01 AM | forum proifle service
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

You are so professional and your fact about this good topic is really supreme. Will you continue your work? We should purchase the format thesis and thesis from you.
1/21/2012 2:18 PM | thesis writing service
Gravatar

# Writing

Nice Blog,I Like For Putting This Together! "This is obviously one Nice post. Thanks for the valuable information and insights you have so provided here. Keep it up!"
1/24/2012 3:20 AM | Writing Help
Gravatar

# Seo Orlando

the existing DOM Level 2 Event model and any EventListeners registered using DOM Level 2 Event model methods.
1/27/2012 7:49 AM | Seo Orlando
Gravatar

# re: Allowing for Dynamic Embedded View Substitution with MVC

looking for more of such topics and would like to have further information. Hope to see the next blog soon.
1/28/2012 2:52 PM | Bolig i Thailand
Gravatar

# London Web Design

loading models for the embedded views? I'm running into the exact same issue. Thanks! as
1/31/2012 2:14 AM | London Web Design

Post Comment

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

Powered by:
Powered By Subtext Powered By ASP.NET