site.Master file does not esist - asp.net

Probably a silly question, being new to development, I am following tutorials and find references to a site.Master file in many of the tutorials I have come across. Is this something that is autogenerated or must I create this file myself? I have access to vs2010 and 2012 and I don't see a site.Master file in any of my projects that i have started. There is however a _layout.cshtml file. I only ask as in every tutorial that mentions this file, doe not mention creating it, just that the file exists.

Here is the deal.
You are probably reading tutorials about MVC or MVC 2 where the view engine is aspx and master pages still are used as a template.
Since MVC 3 a new engine is introduced: Razor. Also this _Layout.cshtml page takes the role of Site.master (master page). With Visual Studio 2010/2012 if you select an MVC project it defaults to Razor syntax and includes _Layout.cshtml as a Shared View.
You can still follow these 'old' tutorials, but mind this difference and act accordingly when recreating the steps.

It could be auto generated if the template you created your project from included a master file. Look in the Solution Explorer (If the solution Explorer isn't visible, hit View -> Solution Explorer) and see if you have a file in your site that ends in the extension ".Master". If not, right click your project in the Solution Explorer then click Add New Item. On the left select your language (Visual Basic or C#) then in the right select Master Page. Give it a name at the bottom such as site.Master. Then click Add. You'll have a master page.
After that, you'll probably want to hook up your other pages to use the new master page. But I'll leave that to your tutorials.

ASP.NET Master Pages: http://msdn.microsoft.com/en-us/library/wtxbf3hh.ASPX
site.master files are usually auto-generated for you when you make a new default ASP.NET website in Visual Studio. Depending on where are in your project, you may need to create one yourself, if Master Pages is the route you want to take. They aren't mandatory, they just make things easier IMO.

MVC has no concept of a masterpage, and site.Master doesn't exist. If you find a reference to site.Master on a tutorial you are using 'regular' ASP.NET (or, as I like to call it, if I'm trying to be polite 'old-fashioned' ASP.NET), not ASP.NET MVC

Yes , It is auto-generated when you create a new ASP Website Project in VS

Related

Asp.Net Publishing Issue

I am publishing my website in asp.net but i am in a small problem i will explain you my problem with screenshot
This is my Solution Explorer as you see I have 2 ApprovalListing pages one in my Employee Folder and Other in my Order Folder
Now i publish my website using Publish Wizard and used Use Fixed naming and Single Page Assemblies
and Now the problem is as i have told you i have 2 ApprovalListing in my Solution and when i publish my website which dll is my EmloyeeApprovalListing and OrderApprovalListing. Can any 1 point out how can i check that out ?
Open each corresponding aspx page and look at the reference on the top. It will say which DLL is used by each page.
For example, one page will say something like:
...inherits="ApprovalListing, App_Web_about.aspx.cdcab7d2" %>
where cdcab7d2 will correspond to one of the DLLs

ASP.NET custom templates, still ASP.NET controls possible?

Hello: we currently do not use asp.net controls (no web forms). The way we do is:
1> Read HTML file from disk
2> lookup database, parse tags and populate data
finally,
Response.Write(page.ToString());
here there is no possibility of using asp.net controls. What I am wondering is, if we use asp.net controls in those HTML files, is there way to process them during step 2?
Thanks and appreciate your response.
I haven't tried this but you might want to attach the html extension to the ASPNET ISAPI filter in your IIS and in your page, step 2, use Server.Execute and call out that html file. However that page will execute on its own.
If these html pages from step 1 are meant for making up parts of the page that needs to get inserted in parts of the webform (.aspx), I suggest that you make use of master pages instead.
If the html pages are standalone pages that need extra functionality you can simply upgrade them to webforms without codebehind if needed. Custom made macros in Visual Studio can help a great deal in this transition effort.

ASP.NET webpages without names, like stackoverflow?

Mentioned stackoverflow only as an example, but if you look above the URL for ask is
http://stackoverflow.com/questions/ask
which means /ask is a subdirectory, but they also do this for the specific question pages. How do you code this in .NET?
Not a code question as much as a technique. I know this is great for SEO, but how do you create a site so that every "page" is its own directory? Dynamically.
Do you have a template or a hidden redirect???
How?? :)
It's termed URL rewriting:
Url Rewriting with ASP.NET
MSDN: URL Rewriting in ASP.NET
EDIT: As #Justice points out, StackOverflow uses Routing.
StackOverflow uses something called Routing, which comes with .NET 3.5 SP1. Routing is a popular feature of a number of MVC frameworks, such as ASP.NET MVC, Ruby on Rails, and a number of Python and PHP frameworks.
Stack Overflow was built using ASP.NET MVC which uses a technique called Routing, see:
What Was Stack Overflow Built With?
and Routing
Stack Overflow uses ASP.net MVC
MVC uses the URL + Query String to determine the content, so its not like a URL which points to a specific page, but more like a hierarchical path to the properties of some data to be displayed
E.G. https://stackoverflow.com/users/[Put User ID Here]/[Put User Name Here]
prompts the website to display a USER with an ID specified in the path ( in this case the user name is probably just for kicks ) as opposed to a specific page created just for that user.
I have seen this accomplished by simply creating a folder for every web page and then having each folder contain a Default.aspx document (Assuming Default.aspx is setup as a default document in IIS, which it is by default). Then you can navigate to any folder on the site without specifying the page (Default.aspx).
For the dynamic part, I have worked with CMS systems that do it this way and the Default.aspx page simply inherits from some master template and the CMS system utilizes the ASP.NET rendering enginge to dynamically complete the web page.
Using folders may be a little heavy with the site structure, but it is an easy way to eliminate the page names from the browser.
This is how I structure my website and avoid having to use page names... for example http://www.innovaapps.net/Blog simply brings up the default.aspx page without having to specify the page name.

ASP.NET Master Page in separate assembly

I have some ASP.NET Master Pages located in one assembly. I need to use these Master Pages with my WebForm pages located in other assemblies that have a reference to the first assembly. I cannot seem to figure out a way to do this.
Is there a nice way that I can do this?
If no pretty solution exists I would also like to hear about possible hacks?
Master Pages are based upon Usercontrols, therefore they cannot be shared across applications.
That said, Dan Wahlin has a way around that particular limitation listed on his blog.
Sharing Master Pages Across IIS Applications by Dan Wahlin will give you some ideas..
Also, check this excellent article by Scott Allen. You can check the last section at the end on sharing master pages.
There are no pretty solutions to this problem.
What you can do is put it into a separate web application project and pre-compile it. Then take the precompiled dlls and ILMerge them into a single dll that you can reference in your app.
The actual master page that you reference will have a class name like this: MyMasterPage_aweknk so look it up in reflector. The class that looks like "MyMasterPage" is really just the code-behind.
It's really disgusting, but it's what we've done to reuse user controls in multiple applications.

How to consolidate ASP.NET master pages across applications?

First shot at throwing a question on these boards so hopefully I can get some help, here goes:
I am working to start up the .NET practice at my client. We have 5 small scale .NET applications in place currently with a few them of them live into production. They're mostly small reporting pieces with some data entry/business logic functionality. Each of these applications is currently using the identical master pages.
What I mean is that there is a copy of the same master page in each application. They are all basic website->WCF->BL->DB tiered applications. So I have 4 copies of the same master page that I have to change when I make a change to it.
The client DOES NOT want to consolidate all of these into a single solution. They like the separation of applications across sites. I just don't want to continue dealing with the hassle of multiple updates for common elements (which there will be many more of across these applications).
The code is all stored in team foundation server. We also do NOT want to compile the master page into a .dll and deploy it.
Can anyone please make some suggestions as to how I can maintain a single copy of these common files (master, .css, etc) across my multiple applications.
thanks in advance
You might want to look at Sharing Master Pages in Visual Studio.
If that is no help then you could try using Build Events in Visual Studio. I would pick one of the projects to be my "Main Project" and only edit the master page from that project. When you build the project it would run a command that would copy that master page(if it had changed) to your set locations.
The client DOES NOT want to
consolidate all of these into a single
solution. They like the separation of
applications across sites. I just dont
want to continue dealing with the
hassle of multiple updates for common
elements (which there will be many
more of across these applications).
The code is all stored in team
foundation server. We also do NOT want
to compile the master page into a dll
and deploy it.
You eliminated the only two real options there. What all is in the master page? Would it be possible to extract the HTML UI elements to a single template or series of template HTML files and import those dynamically into the master page? You could then relocate the common HTML to an arbitrary URL and have the master page for each application pull it in dynamically.
Edit: I lied. You could also use a VirtualPathProvider like Sharepoint does to store the master page in a database or some other directory, but beware that VirtualPathProviders do not work in MediumTrust environments.
See:
http://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.aspx
If you are using Web Applications (compiled into a dll) rather than Web Sites you can do the following:
Right click on the folder you want to store the master page in
Select "Add Existing Item..."
Browse to the master page on the file system, and select both the .master and the .master.cs files.
Then, rather than clicking on the "Add" button, click on the little down arrow to the right of Add, this will bring up a little menu with the options: "Add" and "Add As Link"
Select "Add As Link" this will reference the file in your project, while leaving it in the original location in your dev environment - this allows you to edit it in either application, while keeping it up to date in the other applications.
Obviously if you edit the code behind, you'll need to re-compile the other projects before you deploy the changes to those sites.
This isn't available in web site projects as they rely on the file structure to work out what is in the project.
EDIT: Missed the css part. Obviously you won't be able to serve those files, so this should only work for the master page.
Don't know your scenario, so
IF you can control the DNS / virtual directories to the applications you could use a format like this:
c:\inetpub\wwwroot\Application1
c:\inetpub\wwwroot\Application2
c:\inetpub\wwwroot\Application3
c:\inetpub\wwwroot\Application4
c:\inetpub\wwwroot\Application5
and have your Master page at c:\inetpub\wwwroot\master.Master,
c:\inetpub\wwwroot\master.Master.cs,
c:\inetpub\wwwroot\master.Master.cs.designer
Then you could reference the single copy of the master page from /../master.Master. I gave this a quick shot with a precompiled master page to make sure I could reach back beyond my root. You might have to give it a shot to see.
We use our source control to create links to the shared files in all the places that we need it. So if you edit in one place, you just need to do a get latest and it will appear in the other places you have linked it.
I ended up going with the VPP route. I created a virtual path provider and built my master page into a DLL and this is working. Now I have a massive problem though in that a Content page whos master page is late bound through the codebehind throws validation/formatting hissy fits because it thinks its should be a stand along page. my CNTRL + K, CNTRL + D has broken on every page where I'm now sharing my master page. This is extremely frustring for me and the team

Resources