ASP.NET MVC 3, Razor Views, and Portable Areas - asp.net-mvc-3

I am trying to using portable views with ASP.NET MVC 3 and razor views as that seems like the best way to create an easy plug-in architecture. So I have my class library setup and I have my view located in /Views/Admin/Index.cshtml and it is set as an Embedded Resource. I then include that project as a dependency for the main web application project. When I try to access the Admin controller, Index action I get a message that is can't find that view file (so the controller is being properly included). I am assume it is trying to look in the main web application project and not the portable areas binary. Is there a way to get razor views to work with portable areas?

I have been struggling on this particular issue for a while, but I think I finally figured it out.
The folder structure and how the namespaces are called inside your project is very important for this to work properly!
I have a working example of a Portable Area with embedded razor views here:
https://github.com/fretje/MembershipStarterKit
Take a look at the structure of the project.
The area's name is UserAdministration, and there is a UserAdministrationRegistration class in the root of the project, which resides in the UserAdministration namespace.
Then there's a Controllers, Models and Views folder (just like a normal MVC project) and under the Views folder, there's again a UserAdministration folder which contains the views for the area.
Also something else which is very important for the embedded views to work: you have to register a new view engine in the Application_Start method of your global.asax.cs file, did you do that?
PortableAreaRegistration.RegisterEmbeddedViewEngine();
And... In your registration class, make sure you override the RegisterArea method which takes 2 parameters (AreaRegistrationContext context and IApplicationBus bus), and call the base implementation in there:
public override void RegisterArea(AreaRegistrationContext context,
IApplicationBus bus)
{
base.RegisterArea(context, bus); // <== very important!
context.MapRoute(
"UserAdministration",
AreaName + "/{controller}/{action}/{id}",
new { controller = "UserAdministration", action = "Index",
id = UrlParameter.Optional }
);
}
If you don't call the base implementation, you have to at least add a
RegisterAreaEmbeddedResources();
To make sure that your embedded views and resources are registered.

I got this working by following the instructions in Fretje's answer and then also add a nuget package reference to EmbeddedResourceVirtualPathProvider in your website.

Did you make sure that you Marked your View as Embedded Resource in your Portable Area?
Also i found that nice feature of portable areas is that you can override the embedded Views, so if you Place a View in your Host App with Same name and Location of the Embedded one with Different Code Logic it will take priority over the Embedded one Nice !!!
Hope this Helps

Related

How I create controller folder in mvc6

I Created empty project in mvc6 but theres is no mvc folders , how i ceate model,view,controller folders ?
Because an Aspnet core empty project is truly empty. It is up to you to go about making it.
If you notice it's not just the folder but some of the most used middlewares will also not be there such as app.MVC and app.usemvc in your startup.cs class. The purpose of an empty project is to give you complete control over what you need or don't need.
If you know you definitely need MVC then I suggest you use the ASP.Net MVC template that comes with all the folders and goodies you will need to run MVC.
As for MVC folder structure; controllers and models don't need any special folders. That's just a convention. But views do. So you need to have a Views folder at least.

How do I change Empty Template to Internet Template in MVC 3?

A new MVC Web application has been created with Empty Razor Template. In this template I found that default controller,Model and View are missed. I need to change the template from Empty to Internet Application.
I couldn't find any option in the properties to change Internet controller. How can I do that without creating a new project?
You could create a new project in reverse. Create a new project based on the Internet template, and use a compare tool (I rely heavily on BeyondCompare) to merge the Internet template differences into your existing project.
The difference between Empty Razor Template and Internet Application Template is in two (Account and Home) Controllers, Views that go with them and in different Site.css. You can't simply "switch" between templates.
You can go from Internet Application to Empty, if you delete Account and Home Controllers and corresponding Views. Other way around is a bit more difficult. Use scaffolding to accomplish that.
You can also check differences between templates here.

ASP.NET MVC 3 Cannot find _viewstart.cshtml when using plugin/embedded views

I'm trying out a plugin mechanism for ASP.NET MVC 3 (razor) using embedded views as described here, I changed the view engine to inherit from the RazorViewEngine, but otherwise didn't really change anything. Now I'm calling a controller action in the plugin assembly, which renders a view, this works ok. But the view cannot find the _viewstart.cshtml, and thus no layout. Ideally I'd like to let the host application define the viewstart and layout. Possibly I shouldn't directly call a plugin controller action though, but rather only render partials from the plugin and let the host application handle the main controllers/views.
These are the viewstart locations that are tried in my own VirtualPathProvider:
"~/Plugins/MyMvcApplication.dll/_ViewStart.cshtml"
"~/Plugins/MyMvcApplication.dll/_ViewStart.vbhtml"
"~/Plugins/_ViewStart.cshtml"
"~/Plugins/_ViewStart.vbhtml"
"~/_ViewStart.cshtml"
"~/_ViewStart.vbhtml"
I'm also considering just forgetting about embedding views as it seems a bit fiddly, so I might just opt for copying views to the host application, which could make debugging easier for future users. The risk is that the users will make changes to these views, making updates of the plugin harder. One of the reasons I have doubts about the plugin mechanism is because of possible performance implications, though I haven't done any measurements yet.

what is the correct approach in order to host / integrate / show my existing MVC3 project inside orchard?

I've an existing MVC3 project that implements a certain functionality, this project has it's own views, and a separate Database.
now I'm required to use the same functionality inside one of my orchard project,so I thought that I can host this solution in somewhere and view it inside an iframe or something.
Am I thinking right?,
is this the correct step to take in order to achieve this requirement inside Orchard?
to make it more clear, all I need to do is to view this solution and interact with it's controls and views from a hosting page inside orchard, and the subsequent requests should be handled by my solution in order to hit it's own data store and get back with the requested data in order to be displayed to the user.
any help would be appreciated.
Update:
thanks for Bertrand Le Roy for his answer, I can now view my solution inside my
orchard website.
I came in to one more HUGE problem, which is that my application can no longer connect to my external database.
I've a DB that is hosted in some where else, and I'm using EntityFramework to deal with it.
the problem is that if I put the connection string inside my module web.config, or main orchard web.config, I run into several types of errors like:
"System.Reflection.TargetException: Object does not match target type."
or
"System.Data.MetadataException: Unable to load the specified metadata resource."
My question is: How could I pass my connectionstring correctly to my solution, assuming that I'm using Entity framework as my ORM.
Many thanks.
You will need to put it into a module.
You will have to move route definitions to a Routes.cs file (look at any existing such file for examples).
You will also need, in order to access your data store, to opt out of the ambient Orchard transaction around the data access code (using (var scope = new TransactionScope(TransactionScopeOption.Suppress))).
If you are using dependency injection, you may have some work to move that to the Autofac-based way of doing things in Orchard.
If you want your work to appear seamlessly in the Orchard admin, you may want to decorate your admin controllers with the Admin attribute. If you want your front-end to use the current theme, you'll have to add Themed attributes and maybe refactor your views so that they only emit HTML for the content zone instead of for the whole page.
Add a manifest (module.txt) to your module folder and you should be good to go.

best way to multi-language asp.net mvc 3

I'm trying to create a asp.net mvc3 project for a academic project, and one of the requirements is it has to be able to change between different languages. Currently what i have is the following:
I have a external project that works as a repository for languages and for each view i have an interface for each view that defines all the "placeholders" do define all the changeable text.
At the beginning of any action i obtain the language that is in the uri (something like /{lng}/{command}/{action}) and pass it to the view using the ViewBag, once inside the view i user the repository to obtain the current implementation of the interface for that view in the chosen language.
I can't find any good topic on this mater. I'm just curios if there is a better way to do this and more efficient. And how is it normally done in a professional level.
I'm not very experienced with asp.net just started learning it about a month ago.
Also if it's important i am using the razor engine for the views, and we can't use any JavaScript in this phase of the project.
You may go through the following guide.
I'm working with a project called Griffin.MvcContrib which has some localization features.
First of all, I use the query string and a cookie to switch language. (Just create a link with a flag in your layout English)
and tag your controller with my attribute:
[LocalizedAttribute]
public class YourController : Controller
{
}
The next thing is to get localization of views, models and validation messages.
The localization of models and validations are described here. As for views, you only need to use #T() to get translated texts:
#Model.Title
<div>#T("This text will get translated")</div>
(you need to change pageBaseType in Views\Web.config to Griffin.MvcContrib.GriffinWebViewPage)
I'm almost done with an adminstration area that any non-technical user can use to manage all translations. Check the Griffin.MvcContrib.Admin project here: https://github.com/jgauffin/griffin.mvccontrib/tree/localization/source/Griffin.MvcContrib.Admin

Resources