MVC4 Portable Areas? - asp.net-mvc

Does MVC4 remove the MVCContrib requirement to achieve portable areas? Or is that still required?
I gave it a shot w/o MVCContrib and unfortunately I'm stuck with a typical "The view 'Index' or its master was not found or no view engine supports the searched locations." exception.
The list of searched locations includes "~/Areas/Inbox/Views/Default/Index.cshtml" which is technically correct, but it's just not finding the view.
Some explanation:
I have a main MVC4 application and a "plugin" (if you will) MVC4 app that contains an area called Inbox with a controller and view named Default. I reference this plugin app in the main app and I can hit a breakpoint in DefaultController's Index method where the View is returned - so I know the main app is engaging the plugin app and routing the requests to the proper controller and all that. When not running, if I cursor over the return View() statement in DefaultController the balloon shows the desired view, it's just not found at runtime.
Has anyone encountered this? I'm sure it's something really simple and a stupid oversight on my part, but after hours of trying, I can't find a solution. Thanks in advance!

MvcContrib is not a part of MVC, so no. MVC4 does not include Portable Areas. MvcContrib is not like MvcFutures where it's likely that at some point the code will be rolled into the main project. This is code that is contributed by third parties.

For each view, edit the Properties and set the Build Action to Embedded Resource.

Related

How can we check if MVC view is partial or not in solution explorer

I am working on an existing Asp.net MVC 4 project and I want to know if there is any way to check in solution explorer that if view is partial or not. Please suggest.
Partials often have an underscore prefix in their name. Other than that, nothing really separates them from other views.

ASP.Net MVC 2 Areas: The partial view '...' was not found

We recently upgraded a project to MVC 2 and we'd like to use Areas but there is an issue.
We have created a new area, setup a controller, configured a route, and created a view in the correct location. When we run the code it successfully finds the route and hits the controller but when it goes to render the view there is an exception.
The web forms view engine doesn't seem to be looking in the Areas section for views. The error we're seeing is:
~/Views/<ControllerName>/<ViewName>.aspx
~/Views/<ControllerName>/<ViewName>.ascx
~/Views/Shared/<ViewName>.aspx
~/Views/Shared/<ViewName>.ascx
When it should be:
~/<AreaName>/Views/<ControllerName>/<ViewName>.aspx
~/<AreaName>/Views/<ControllerName>/<ViewName>.ascx
~/<AreaName>/Views/Shared/<ViewName>.aspx
~/<AreaName>/Views/Shared/<ViewName>.ascx
~/Views/<ControllerName>/<ViewName>.aspx
~/Views/<ControllerName>/<ViewName>.ascx
~/Views/Shared/<ViewName>.aspx
~/Views/Shared/<ViewName>.ascx
This would indicate that it's still somehow using the MVC 1 dll but we've looked carefully and can find only references to the V2 of MVC (there was a V1 reference in xVal, a third party DLL, but fixing that didn't make a difference).
I can only imagine that we missed something when we did the upgrade or that we've got some unusual edge case because there doesn't seem to be anything on the web that matches the problem we're experiencing.
What things could we look at that might help us resolve this issue?
Thanks in advance for any help provided.
Cheers,
Zac
What things could we look at that might help us resolve this issue?
Make sure Area Routes are Registered AreaRegistration.RegisterAllAreas(); are registered first. Area routes need to be registered as well.
Make sure generated URL links have the area name included as one of the arguments when using ActionLink and other related URL helpers
Sometimes just delete and re-add the Area from scratch. Sounds silly, but it works.
The related SO question on the left hand side of this page may help. I have linked one I think may be of value

Changing ASP.NET MVC default folder structure

Is it possible to change around the folder organization to the following and still have MVC work.
P1
Controllers
Models
Views
P2
Controllers
Models
Views
etc..
This looks a lot like the new feature area's which is available in MVC 2 though it is currently in preview and will be release with VS 2010.
If you want to do it yourself without using areas then check out this article on view engines
as said before, the Areas feature is what you are looking for.
If you are using vs2010, then although you have MVC2, you don't have the tools for creating the areas - these are only available on vs 2008, when you install the MVC2 rc
to make it happen follow the instructions at the following link: http://msdn.microsoft.com/en-us/library/ee671793%28VS.100%29.aspx
as you can see the menu options are missing so:
download the example project from there.
build the directory structure at your solution according to the example
change the AreaRegistration.cs to match your area name
add AreaRegistration.RegisterAllAreas(); at the RegisterRoutes function at the global.asax, right after the IgnoreRoute
NOTE: the namespaces shuold contain .Areas.
for More explanations search at stackoverflow for asp-net-mvc-2-beta-single-project-area-registration-getting-http-404 (since this is the first time i post an answer - SO doesn't let me append more then 1 link, go fugure..)
Hope this helps
Should be doable, I think the default folder structure is just a suggestion. I've seen before in some documentation that larger projects would likely be split up differently, possibly even among multiple assemblies.
The only thing I would think would cause some trouble would be the views. Since controller actions are mapped to them only by file name. I know there's some way to change them, but I have not had to do so yet.
You can create your own viewengine to solve the paths. That sounds like a very strange idea, but the ViewEngine class is actually responsible for 2 things: locating a view and rendering a view to the httpresponse. The thing you have to change is the locating part, you can just inherit the default aspxviewengine and change the view locating part.

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

ASP.NET MVC RC - Creating a MVC User Control with a codebehind

Trying to create a MVC User Control in the Release Candidate and I can't see to make one with a codebehind file. The same is true for MVC View pages.
Creating Views in the Beta would produce codebehinds...am I missing something?
Code behind kind of defeats the purpose of the MVC Framework. Functionality should be kept separate from the view, the MVC team felt that code behind pages went against this ideology and therefore removed them.
Your can create a custom helper method to create your control. Also I'm not sure if MVC has view components (Monorail/Castle) but that could be an option as well.
From ScottGu's Blog post:
*Views without Code-Behind Files
Based on feedback we’ve changed view-templates to not have a code-behind file by default. This change helps reinforce the purpose of views in a MVC application (which are intended to be purely about rendering and to not contain any non-rendering related code), and for most people eliminates unused files in the project.
The RC build now adds C# and VB syntax support for inheriting view templates from base classes that use generics. For example, below we are using this with the Edit.aspx view template – whose “inherits” attribute derives from the ViewPage type:
One nice benefit of not using a code-behind file is that you'll now get immediate intellisense within view template files when you add them to the project. With previous builds you had to do a build/compile immediately after creating a view in order to get code intellisense within it. The RC makes the workflow of adding and immediately editing a view compile-free and much more seamless.
Important: If you are upgrading a ASP.NET MVC project that was created with an earlier build make sure to follow the steps in the release notes – the web.config file under the \Views directory needs to be updated with some settings in order for the above generics based syntax to work.*
I answered this question here:
How to add a Code-behind page to a Partial View
Seems this wasn't particularly tricky, and is quite do-able
This answer worked for a Partial 'ViewUserControl' but the same should apply
Ok.
First: Add a Class file with the convention of .cs (i.e. view.ascx.cs)
Second: Add "using System.Web.Mvc;" to the class
Third: Change the Class to Inherit from "ViewUserControl<>"
Fourth: Add the following to the View's header:
CodeBehind="View.ascx.cs" Inherits="Project.Views.Shared.View"
Fifthly: Copy the files out of the solution and drag back in to reassociate the two together
Note: For this to work with a Normal MVC View you just need to inherit the class from "ViewPage"
The whole idea for ASP.Net-mvc was to get rid of the codebehind files...thats why asp web controls didnt matter that most didn't work.But with the changes of getting rid of the code behind comes with a different programming style..The idea is codebehind files are EVIL:
http://stevesmithblog.com/blog/codebehind-files-in-asp-net-mvc-are-evil/
the whole idea is to make sure people remember they are using asp.Net-mvc and not asp.et web pages. take alook at this link ,it explains it a little better:
http://blog.lozanotek.com/archive/2008/10/20/Visual_Studio_Templates_for_MVC_Views_without_Codebehind_Files.aspx
I think this tutorial is what you are asking.. but not really sure what you want..

Resources