Reference MasterPage in HTML file instead of ASPX - c#

I have some pages written in cshtml but they don't use any of the razor features. Basically they use knockout javascript and then query MVC controller methods for get and post.
I don't want to use any WinForms features, I just want to make AJAX calls.
I want to use these pages in a WebForms project but want to know if there are any issues with just using plain .html files inside a WebForms project. Will this cause any major issues with the Web Forms project? Can I still reference a MasterPage?

You can use HTML files in you WebForms project as well in ASP.Net MVC project as long as you don't need any server side processing of such files...
But using master pages requires server side processing, so plain HTML files would not work for that.
As an option you can use such files in IFrame rendered by regular ASPX page or just include all content onto ASPX page (read file and write without encoding).

Related

Implementing an html/css framework with a master page in visual studio

I'm working on a website for a school project and I designed it in photoshop with 9 columns.
To make life easier I want to use the 960 grid system (Custom for 9 columns) in order to build the design in html/css.
Part of the projects requirement is that I need to use a master page and web forms in c# however I'm not sure how to implement the 960 framework within that.
does anyone have any experience with this?
It sounds like you're being forced to use ASP.Net Web forms. I'd typically recommend you use ASP.Net MVC instead, and it does support Master pages, but really everything is a view, and I expect you would get push-back from the professor. So, to keep with the standard ASP.Net Web forms approach, you should create a new ASP.Net WebSite, and check off that you want a master page. Then Visual Studio will create the Master page for you, and any new aspx pages you create you can click a box to have them inherit from the master page.
If you look at the .Master page, you'll see the same HTML you would see on any site, including the , and tags. You should just include the .css file that contains your 960 framework file, just like you would on any Web page. Then the rest f the page will use the 960 grid, and you can put the necessary col# classes inside the class tag (or CssClass for ASP.Net controls) to get the display to work correctly.

Converting MVC2 to MVC3 and pages to Razor

First and foremost reason to update big old MVC2 project for me is the Razor engine. Now following this instructions
I successfully converted the project without yet touching .aspx and .ascx files.
1) Can you guys tell me now, how can I convert Master pages to layouts, and start converting pages and user controls without breaking any existing MVC2 pages? (it's a large project, can't do them all at once). Can I somehow use old master pages as layouts in razor pages?
I converted a couple of partial aspx pages which don't use a master page.
I used Telerik's aspx2razor converter - it helps although you still have to do a lot of manual revisions.
2) Is there any other better tool out there?
You can use Razor and the normal views side by side. By default, MVC3 looks for razor first, then will fall back to the WebFormsViewEngine.
So you should have no problem, and you can tackle each page in time as you see fit.

How do you return an embedded resource .aspx page via a httphandler?

Pop quiz hot shots...
I have a Visual Studio 2010 .NET 4 solution with 2 projects, The first project is a c# class library that contains a httphandler and a .aspx page. The .aspx page's build action has been set to "Embedded Resource".
The second project is an asp.net web application which references the first. The httphandler is wired up in the web.config.
I want the httphandler to serve the embedded .aspx page. How do I do this?
Thanks,
James
maybe this is relevant: http://www.west-wind.com/weblog/posts/2007/Jul/23/Loading-an-ASPNET-Page-Class-dynamically-in-an-HttpHandler
.aspx is just a specialized kind of HttpHandler in .NET. Don't forget that.
Thus, .aspx files (ASP.NET Web Pages) actually have implemented IHttpHandler and they have ProccessRequest method. There are two ways to do this:
Based on dynamic compilation nature of Web Forms and markup vs. code-behind, if you want the markup of the page to be compiled dynamically and be executed, you have to extract the page (through code) and save it on the disk. This extraction process can take place on Applciation_Start event.
If you don't like the extraction method, don't forget that you can remove markup entirely and do everything in code-behind (just like PHP or old ASP or ASP.NET MVC). Also remember that your page is actually a class from the point of OOP. Thus simply instantiate it in your HttpHandler and call it's ProcessRequest method, passing the current HttpContext into it.
The way I would do it is through a VirtualPathProvider, not a handler. You can set up and register a virtual path provider to serve pages from an embedded resource (or database, web service, or anything else you can think of).
http://support.microsoft.com/kb/910441

Move baseclasses for MasterPage from MVC2 to MVC3

I am working on moving an application from MVC2 ASPX to MVC3 Razor, and is quite stuck moving a baseclass for more MasterPages in old MVC2 application.
The baseclass is used for automate include of css and js on pages in order to ease quickfix and debugging when developing application in local environment, but when running application in production environment it has to update and include single minimized css and js files delivered from a external CDN.
The code needs to know about the View file eg. "~/views/home/index.chtml" and/or the Layout file eg. "~/Views/DefaultNoLogon.Master" in order to include and handle css and js files correct.
I tried to implement own baseclass using the pageBaseType in Razor part of web.config, but it seems like it is executed both for View and Layout file, and I could not find a execution point where information about both View and Layout file is present. I also tried to implement the file logic using a HtmlHelper, but I can only access information about the View file and miss information for Layout file for View.
I don't want this kind of code to be implemented in Route, Controller or ViewModel since it should be related directly to generation of Views.
Any ideas how to get information about View and Layout files in MVC3 Razor app?
Well, never mind my question.
I redesigned my logic for automatic rendering of js and css files, which actually is more simple and works better. My Mvc3 Razor app is now capable to take all js and css files for a view (including css and js for all layoutpages) and render them into single minified files.
Works like a charm, and I guess it's more future proof than my old solution, unless Guthrie make fundamental changes in layoutpages and Html helpers in Mvc4 or later.

ASP.NET C# Properly Generating HTML for Page

I realize this is probably a fundamental thing I should know but I am self-teaching myself C# and asp.net so I am a little lost at this point.
I right now have 2 pages. One is an .aspx (with aspx.cs file included) that is blank and html is generated for it from a Page_Load function in the cs file. The HTML is very simple and it is just an image and some text.
The second file is a shtml file which has lots of things, serverside includes, editable and noneditable areas. I want to put my webapp into this file. My asp.net app uses Response.Write to just write out the html. This does not flow well with this page as all that does is write it at the top of the page which is because it is ran first and generates it at the top.
How can I make it to where I can generate HTML code inside the page, like within a specific DIV so it does not mess up the page. Where would a starting point be in learning how to do that.
I should note that I do not need any interaction from the user. All of this should generate right away.
I think you need to read up on some basic ASP.Net documentation and tutorials. Response.Write is not the correct approach - you need to understand how the ASP.Net page lifecycle works and how WebControls are used to render the html.
ASP.Net tries to abstract away having to create your html manually for the most part.
So if i have understood the questions correctly.
You already have an existing page/application (the shtml file) that you want to extend with some new ASP.NET components by including output from the ASP.NET page in the existing page?
This is as not something that is out of the box "supported" by ASP.NET and you "won't" be able to execute the aspx page using SSI. But you can do the opposite, an ASP.NET page does support SSI. So if you are not using any other scripts in the shtml file this might be a solution.
Otherwise the only common solutions would be either to use an AJAX framework and let it call the ASP.NET from within the existing pages or to use an iframe solution. In both cases the client will be resposible for making the calls to the ASP.NET pages and merging the results.
And then you have a issue with controlling the output from the ASP.NET page?
The Polymorphic Podcast has a good article on Controlling HTML in ASP.NET WebForms .
You can add a Literal control to the page inside the div:
<div>
<asp:Literal ID="litMarkup" runat=server />
</div>
then in your code-behind:
litMarkup.Text = "<strong>Your markup</strong>";
I don't know how well this would work for you, but could you try using an iframe to house the ASP.NET page? This should keep it in the specified region and not overwriting your shtml file. It may be something to think about.
If it is necessary that you generate your HTML output from C# code, and you would use this in more than one place, I think you may be thinking of something like what are called ASP.NET Custom Controls (not to be confused with "User Controls"-- though you probably could put together a solution with those as well, using a Literal control as another person suggested). The MSDN documentation would be a good starting point. In general, though, the writing-out-HTML-yourself-from-code model (like you would with, say, CGI applications), is not the usual ASP.NET model of development, as it largely defeats the point of using ASP.NET at all. You'd mostly want to do this sort of thing if you are writing your own web control, though this might be exactly what you are doing (hard to tell from the description).

Categories

Resources