Configurable default view layout - asp.net-mvc

In Asp.net MVC Razor we can use the _ViewStart.cshtml file to define the default layout for our views. That is fine, but why did we loose the web.config setting that was available with web forms engine in system.web > pages.masterPageFile setting?
Why is that important? Because web.config allow automatic environment transformations.
Manual solution
Of course we can put the default layout name in either appSettings and refer to that in the _ViewStart.cshtml or if we have application specific configuration section add additional property which makes things a bit more clear and polished...
But is there any other way that doesn't include code?
Asp.net MVC team should add the layoutFile configuration setting to system.web.webPages.razor > pages element, shouldn't they?
The main question is
Am I missing something that is built-in and I don't know about? Something that requires no code at all similar to WebForms engine?

Asp.net MVC team should add the layoutFile configuration setting to
system.web.webPages.razor > pages element, shouldn't they?
Feel free to open a ticket on the MS Connect site.
But while waiting for your ticket to be analyzed by the development teams and probably added in a future version of the ASP.NET MVC framework and the WebPages you could always do the following in your _ViewStart.cshtml:
#{
Layout = ConfigurationManager.AppSettings["Layout"];
}
and then define the layout in your web.config file:
<appSettings>
<add key="Layout" value="~/Views/Shared/_Layout.cshtml" />
...
</appSettings>
Another possibility is to write a custom razor view engine in which you set the layout dynamically.

Related

What is the equivalent of app.config in MVC 4

I have only few projects on my bag. In a previous desktop application when I wanted to store and use some application specific information I was using the app.config file.
Now I work on ASP.NET MVC 4 application and again I want to store some application specific information but this time I'm not sure. I have the web.config file which seems like a good place for this purpose but I'm not sure if it's the right place to store custom information there.
What is the right approach to do this? For example I want to save and extract path to directory on the file system where I'll save all my files. In ASP.NET MVC 4 what/where is the right place to do that?
The web.config file is what web application use where a desktop application uses app.config, and it's a good place to put application specific information.
You can add keys to the <appSettings> tag, and use them in the application just as you would if you put them in that tag in the app.config file. Example:
string path = ConfigurationManager.AppSettings["DataPath"];
Web.config is for ASP.NET and ASP.NET MVC; app.config is for desktop applications and DLLs.
If you need to storie any kind of parameter for your application that you can change without having to recompile, Web.config is the place to go.
Web.config is the web application version of app.config. Web.config seem like a good place for the configuration settings you're talking about.
Sometimes when you have a dll that had reference to desktop app.config, and when you import that MVC application you need to change the configuration data that used to be in app.config now to web.config.

MVC Sitecore project - how to add key value settings on web.config

Quiet new to Sitecore hence this basic question to start with...
I've got a MVC Sitecore project up and running. However, I would like to include some simple configuration values on AppSetting of web.config file. From what I read it's not good practice to mess with Sitecore config file.
Could any one please suggest the best practice to add key value on appsettings?
Should it go under app_config?
If yes, then which folder and what naming convention should I use?
And how does Sitecore reads this file on runtime?
In my current project I've removed all app settings from the web.config and included them like so:
<appSettings configSource=".\App_Config\AppSettings.config" />
I then use SlowCheetah (Which can be downloaded here) to change the settings in the AppSettings.config file if I have environment specific settings.
All changes to the sitecore section of the web.config I add through the include files. You can read more on include files in this post by John West

Disable Umbraco aspx handling

I have a project where I want to use Umbraco only for the backend as a CMS. But I want to disable it completely in the frontend handling my aspx pages for me, and I want to use the API to get the content I want. In other words, I want to create an aspx page manually which will not be handled by the Umbraco engine. Right now, if e.g. you create a test.aspx page and put it in the root folder, it will return 404 because Umbraco will look for a node with this alias.
Ho do I disable the .aspx handling by Umbraco, but still be able to use Nodefactory etc. in codebehind to access the content?
Thanks
Themos
There are a few ways you can override pages so that their URLs are not "caught" by Umbraco, you can do this by modifying the following appSettings items in the web.config file.
To add single files:
<add key="umbracoReservedUrls" value="~/myfile.aspx,~/config/splashes/booting.aspx,~/install/default.aspx,~/config/splashes/noNodes.aspx" />
To add whole subdirectories:
<add key="umbracoReservedPaths" value="~/myfolder,~/umbraco,~/install/" />
You should be able to add ~/ to umbracoReservedPaths which would disable all URL mappings, I just tried it and it seemed to work. But I can't vouch that it'll have no unintended side-effects on the running of the Umbraco CMS.

Two web.configs?

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

ASP.NET MVC Application in Sharepoint 2007 virtual directory

I have MOSS 2007 installed at lets say http://localhost:4999/ and I want to have my custom ASP.NET MVC (1.0) application at http://localhost:4999/mvcapp/ - logic dictates that, in IIS, i should create a new application virtual directory under my MOSS site and point it at my custom MVC app.
I've done this and it works for executing my controllers etc, however, none of my /Content content is being returned! All referenced images, javascript and css aren't retrieved.
If I put this app into it's own site, or a virtual application within a non-sharepoint site, this works fine and pulls down the images, js and css as normal.
Note - I'm creating a new application in IIS, not just a virtual directory and I have no requirement for integrating with sharepoint, I just want it to have the same domain and port number.
Any ideas?
Cheers
Tony
** EDIT **
To clarify - the URL's that are being generated aren't the issue - they are correct and are being generated in the same way as they would be if this wasn't hosted under sharepoint. IE: /mvcapp/Content/Scripts/jquery.js etc.
** EDIT 2 **
More clarification - the MVC app has it's own web.config file - but it appears that when using a virtual directory withn a sharepoint site, many of the handler mappings still get pushed up to the child site (Note: This is a virtual directory configured as a seperate application not just a virtual directory).
Whilst I don't want or need SharePoint integration, I need my MVC app to come from the same domain and port to overcome some cross domain issues (a lot of MVC content is iframed into sharepoint in various ways). So sharepoint would be at http://site and my app at http://site/mvc
I would keep them on separate web sites (MVC and SharePoint that is). You could create a new website entirely for your MVC app, and then through IIS, right click your MVC web site, edit bindings, and redirect the traffic from your MVC website to URL you want.
I set up a couple of WebForms apps to run in much the same way you say that you want: a separate virtual application with its own web.config, etc. I had to tweak the web.config to make it work, though. My app uses things like session state and view state, but I reckon those aren't applicable to your MVC app. As I look at my web.confing, I think this section might be applicable for what you're trying to do:
<location>
<system.web>
<xhtmlConformance mode="Legacy" />
<trust level="Full" />
<httpModules>
<remove name="PublishingHttpModule" />
</httpModules>
</system.web>
</location>
Hope that helps. I also have an <authorization> section in there, but it wasn't necessary to make the app work.
Look at the following:
Configuring Specific Files and Subdirectories (MSDN)
Disabling Configuration Inheritance For ASP.NET Child Applications (Blog)
HTH

Resources