1

I've never used Master Pages before, only completed a couple large MVC 3 projects and a lot of Asp.NET "Web Site" e-mail forms. What I'm trying to do is convert an application in ColdFusion over to ASP.NET. I have 20 separate sub-sites which all pull people's information from a common database. In ColdFusion I simply have each of these 20 pages set one variable and then include a main ColdFusion file which takes that variable and displays the appropriate listing of people.

In ASP.NET, I can't do includes. I'm not using WebForms so #include is out the window. I've read up on using Master Pages but can I use those across projects? I'm visualizing creating a "main" project which fetches the data based on an input variable and then 20 simple, very-lightweight (but completely separate) pages that include/display information from the master project. I do not want to have to create 20 projects for this each of which include a common library, because that would mean if I needed to update something I would have to publish all 10 projects, which is something I'd like to avoid.

Is this possible without doing a Web Forms project and #including .aspx files? The file layout I have now is that the master project is in /sites/masterproject and the sub-sites are like /sites/site1/directory, /sites/site2/directory, so this is why I'm thinking this needs to be separate, hopefully a simple page with an include rather than 20 separate projects.

3 Answers3

1

Try this:

<!-- #include file ="mypage.cshtml " -->
Jonathan Eckman
  • 2,071
  • 3
  • 23
  • 49
  • Will this work for dynamic .cshtml files if my master project is an Asp.NET Web Site? – Vince Ruppert Aug 27 '12 at 18:32
  • This works the same as Response.WriteFile, both "work" but they include the raw code of the included files as opposed to the output after running through asp.net, meaning this only seems to work with static files and not dynamic ones. – Vince Ruppert Aug 27 '12 at 20:13
0

In .aspx page, you can use MasterPageFile attribute in @Page directive to specify master page path.

So, using default project template from VS2008, you would have somehting like this in your pages:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" MasterPageFile="../../masterproject/site.master" %>

(just specify proper relative path, absolute path will probably also do the job)

pkmiec
  • 2,594
  • 18
  • 16
0

One of the ways you can create the same effect in .net is by creating and correctly using Asp.Net UserControls. http://msdn.microsoft.com/en-us/library/26db8ysc(v=vs.85).aspx. They are pretty much pieces of HTML code which can also have .Net code which later you can simply drag and drop into your other pages and the result HTML will always be the same, or it could also be dynamic. People use it the same way they use includes in other technologies.

Good luck.

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
  • If I'm understanding [the posts I'm reading](http://stackoverflow.com/questions/6317317/mvc-3-add-usercontrol-to-razor-view) about UserControls and Razor (or anything not WebForms), this won't work for me. – Vince Ruppert Aug 27 '12 at 20:31
  • Yeah, you are reading the wrong stuff. Try this http://webproject.scottgu.com/CSharp/UserControls/UserControls.aspx – Hanlet Escaño Aug 27 '12 at 20:35
  • This seems like I would create a User Control, and then add it to other projects to use. My goal here is to try and not create 20 projects and include this common User Control, but rather to create one main project and 20 very small, possible 2-line pages whose only job is to set one variable and then include the main project or display output from the main project. – Vince Ruppert Aug 28 '12 at 19:04
  • Then you can create an extra project for the purpose of doing this. The way I almost always work is by creating 3 projects, 1 Web Project (GUI) 1 C# DLL project for the Business Logic (BLL) and one for the Data (DAL). Seems like what you want to accomplish could be on the BLL. Another thing I've done in my projects when I am going to use a function all over the project, but do not want to write it everytime, or to put it on the BLL, is to write a class and inherit from the Page base class, write my public functions, and I can use them in every page I need them. – Hanlet Escaño Aug 28 '12 at 21:18
  • Thanks for your help, but I think I'm just going to bite the bullet and use WebForms for this project. I can easily do what I want using a #include statement. – Vince Ruppert Aug 29 '12 at 15:59