Two web.configs? - asp.net-mvc

ASP.Net MVC applications has two web.configs. One in the root folder and one in the Views folder. Why?

From Pro ASP.NET MVC 2 book:
/Views/Web.config:
This is not your application’s main
Web.config file. It just contains a
directive instructing the web server
not to serve any *.aspx files under
/Views (because they should be
rendered by a controller, not invoked
directly like classic Web Forms *.aspx
files). This file also contains
configuration needed to make the
standard ASP.NET ASPX page compiler
work properly with ASP.NET MVC view
syntax.

One reason is to simplify your views and your pages. You can put the compilation or even the masterPageFile declaration from your views in this web.config, for example.
Phil Haack did a great post on this -> http://haacked.com/archive/2009/08/04/views-on-a-diet.aspx

Related

In MVC what extension should the view file names have?

I have been given a sample MVC project that contains views with extension .aspx
however when I create a new MVC project using the VS2013 ASP.Net wizard the views have extension .cshtml
Are there 2 kinds of MVC project?
Views in MVC refer to either .cshtml files in C# or .vbhtml files in Visual Basic.
.aspx files are webform files and are not views-- this was the initial approach ASP.NET took to make webform development more similar to desktop application development. These pages are generally included in the route list as actual files, whereas MVC uses controller routes that aren't based on existing files (i.e., the url path doesn't match the file and directory structure like traditional html does), which ultimately serve the views. .aspx files can also have code-behind files to separate the html/aspx markup from the .NET code; those files will have either a .aspx.cs or .aspx.vb extension on them. In an MVC app, these files are also likely to have designer files.
One set of files for an aspx file named MyPage may have the following files:
MyPage.aspx
MyPage.aspx.cs or MyPage.aspx.vb
MyPage.aspx.designer.cs or MyPage.aspx.designer.vb
The files in #3 may be hidden until you select 'show all files' in the project, or may not exist at all in a traditional 'web site' project type. I think you have to upgrade to a 'web application project (Wap)' project type before you can integrate MVC, though I may be wrong. All WAP projects should have these .aspx.designer.xx files.
In MVC what extension should the view file names have?
.cshtml unless you have a reason not to use the Razor view engine with C#.
Are there 2 kinds of MVC project?
The relevant answer is that there are many more than 2 different view engines. Razor was introduce in 2010. The Razor view engine is what comes out of the box in the Visual Studio MVC templates. See ASP.NET MVC View Engine Comparison for more info on more obscure view engines that work with ASP.NET MVC.

ASP.NET MVC Displaying Static Help Website

I'm trying to add Help to my ASP.NET MVC project.
The "help" website contains static pages about the features in my ASP.NET application.
I have added the content for this website into my ASP.NET MVC project and have added a hyperlink that will open the Help in its own window.
However, when I try to access the content, the application attempts to route to the Help controller.
How do I display the help website within my MVC application?
I am not sure you can do this within the context of an MVC application. I would consider just building an empty controller with an Index action (HelpController -> public ActionResult Index()) and just return the view name (cshtml file), shouldn't be any reason you can't rename your static html file to cshtml even if you aren't using razor (although I am not 100% sure without trying that the extension change is necessary). Also I would argue that if this ever needs more functionality you have the scaffolding in place to make non-static mods. Disabling routing within the context of an MVC solution honestly doesn't make the most logical sense. The only other choice would be if you hosted it in a different IIS site (but I don't think I would recommend that unless you have a huge help library).
Use IgnoreRoute when you configre your routing, for example, create a folder "help" in your app's root. Then load it with all your html help files. Then to ignore that route:
routes.IgnoreRoute("help");
You should then be able to access it by http://myapp.com/help/whatever.html

Side-by-Side Asp.Net and MVC Razor

We have an existing ASP.Net Web Application. I would like to create an ASP.Net MVC Razor Application where the two applications will work together. A single Master Page would contain menu items that can call .aspx pages as well as Razor .cshtml pages.
I have seen an example using MvcContrib Portable areas utilizing Routing. This particular example has .aspx pages in both (the MVC was not Razor).
Is there an example out there that will show the two running side-by-side and the MVC is Razor? It would be best if I could download a visual Studio Solution so that I can run this.
I am not sure if the MvcContrib way is the latest and best way to achieve this.
I do not want to go Hybrid!
You don't need any other external librarry. You can always convert the existing ASP.NET web forms Project to be a Hybrid one which uses webforms and MVC. You need to add the required MVC assembly references and make some changes to the web.config and you are all set. Scott has a simple and awesome blog post about this where he explains how to do the conversion.
I scribbled a note about how to enable the MVC specific Context menu( Add Controller / Add View) in the hybrid project after conversion here

How can I make ASP.NET MVC 3 use views (aspx, ascx) files from an external assembly in my website?

I have an ASP.NET MVC 3 website and I'm looking for a way to package up a set of views and controllers into an external DLL and hopefully use MEF (Dependency Injection) to load the correct controllers and views.
Does anyone know if something like this is possible and any good links to tutorials on the subject?
Precompiled Razor for Shared Views and Mono
Compile your asp.net mvc Razor views into a seperate dll
A combination of those two will probably get the best result in your case - My precompilation code handles some things the single file generator doesn't, but Chris has more use of out of box code at runtime, which is probably better if you only need to run in a standard mvc3 setup.
I have also used aspx files set up as embedded resources ( Views in separate assemblies in ASP.NET MVC ), but Razor is quite a bit easier to work with in compiled form.

Routing issue when using ASP MVC 2 areas

I am using asp mvc 2 areas. I am trying to set up the project such that when the web site is launced it calls one of the controllers in my areas instead of the default home page.
When I update the routes in the Global.asax file, it doesn't seem to work.
It is giving me an error since it is looking only in the views directory and not in the Areas folder. How can i force it to look into the Areas folder too?
Have you created a custom ViewLocator as described here: http://blog.codeville.net/2008/07/30/partitioning-an-aspnet-mvc-application-into-separate-areas/

Resources