How to access Page.Header.Controls in ASP.NET MVC 2? - asp.net-mvc

Is there a way to access Page.Header.Controls in ASP.NET MVC 2?
Edit
I want to create a helper which can manage scripts.
Yesterday, after I asked this question I discovered, that ViewPage and ViewUserControl has Page property.
I think this allows us to access Page.Header.Controls.

No, Page.Header doesn't exist in ASP.NET MVC. Header is the <head runat="server> control in ASP.NET Web Forms. ASP.NET MVC doesn't use controls like that.
If you want logic in the <head> of your HTML, you have to go about it the ASP.NET MVC way, by using code in your view or your master page.
The Page property of a ViewPage comes from the fact that Web Form Views are bastard Web Forms. ASP.NET MVC normally doesn't use any bits of the Web Forms beside the code-in-front and the markup, but via various unsupported haacks you can abuse it into letting you use other Web Forms stuff. Don't do it.

Related

Using ASP.NET MVC under ASP.NET Web form project - Best practice

Currently we have a new menu item to our existing asp.net web forms application.
We have decided to go with MVC for this.
Please note that we have to share the same masterpages, css and Jquery files. We are currently planning to render the view inside a div in our aspx pages.
Is there a better way to accomplish this?
What are the limitations of our approach?
Can we leverage the MVC test cases in this approach?
Thanks
MVC is a stateless, disconnected architecture, you can not access the master page or aspx controls directly in the controller as aspx.cs does, secondly it is not possible to use the classic asp.net master page with mvc.
The things you can re use from existing project are:
UI Templates (design)
JS files
CSS files
Business Logic
Database
You can't use same master pages. You can't reuse ASP.NET MVC for some parts of your webforms view and vice versa. Either your view is completely on webforms or on ASP.NET MVC. And theoretically even mix of webforms with mvc in one web application could be problematic, but practically possible (see below point 1). So generally answer to your question is no.
If you are looking to ASP.NET MVC, you should think about step-by-step migration of your project to it.
Actually there are a couple of possibilities how to migrate
Mix in one project ASP.NET web forms and ASP.NET MVC. Technically it is possible. But it is some kind of hack. Your transition will be seamless but you can't your ASP.NET views. I will not recommend it. You can find this approach here http://www.devcurry.com/2013/05/adopting-aspnet-mvc-enhancements-in.html
In one solution use different projects for your webforms and mvc projects. In fact these are 2 different web application which typically could have common authentication, and there is question with webforms state. To solve problem with authentication you need create separate web service for it, which will be used by both. To solve problems with session state, use distributed caching for both, and try to change thinking from webforms state to caching, because mvc is actually stateless. Then for particular views you can redirect between views of both portals

Incorporating MVC functionality in an existing ASP.net project

We have an intranet system developed using asp.net webforms. We are kind of planning to partly incorporate mvc and such a scenario is as follows,
We would like to generate html documents using mvc (using razor view engine) where the inputs are going to come from a normal asp.net webform (from a form button click or so)
Some pointers or references to tutorials on running these 2 scenarios side by side would help a lot.
ASP.Net MVC and WebForms postbacks don't mix; the only way to do this would be to use <iframe>s (which is not a good idea).
I am not sure about your described implementation... however, you could start by re-writing some of your aspx pages using MVC views and just get all the existing code behind into a controller to minimize the creation of new code. You could do this one page at a time until all of your pages are MVC views. Good luck.

If MVC2 uses aspx pages and master pages is it okay to use asp.net server controls?

Ive noticed that MVC 2 uses masterpages and ascx pages. I'm used to using Razor pages but have to work on an older project. So I'm wondering if it is okay to use asp.net server controls on these aspx markup pages?
I know its advised not to plug into the page lifecycle but what about user controls?
If you want to use user controls, stick with Web Forms. User controls are not used with MVC. The MVC view engine does not have a page lifecycle like Web Forms.
Your Usercontrols plug in to the WebForms page lifecycle, they maintain their state through ViewState. There are helpers that were created to render out controls to MVC but they are limited in the fact that they are read-only, the controls don't support posting back or any events like that.
http://malvinly.com/2011/02/28/using-web-forms-user-controls-in-an-asp-net-mvc-project/
You could create partials out of your own controls:
Using webform user control on webform in MVC Project

Custom Asp.net server controls that will work in MVC and Web Forms?

Is it possible to develop controls that will work in both Asp.net Web Forms and MVC?
It seems that we're heading for a situation where we now have to maintain another set of code for MVC over and above the code being maintained for Silverlight and Web Forms and heaven knows what else.
It is possible, but not pretty. MVC does not have the ViewState.

Use ASP.NET Web Forms UserControl in ASP.NET MVC?

I've been tasked with making a prototype web application, and i'm debating between using ASP.NET WebForms or the new ASP.NET MVC.
There is a commerical ASP.NET UserControl that i would like to use that gives me 95% of the functionality i need (and it does it in an AJAX-y fashion). But i've heard that since ASP.NET MVC doesn't use ViewState, it can't run these WebForms-based controls.
So, is that true or false?
I'd really like to use this commerical UserControl, but i want to use ASP.NET MVC if i can, and only if ASP.NET MVC is not going to give me much trouble when trying to use the WebForms-based control.
Traditional WebForms and MVC aren't mutually exclusive; you could run both of them in the same site. For an explanation of how to make this happen, see this post by Scott Hanselman.
So you could, for example, create WebForms-based page(s) to leverage the commercial control, and use MVC for everything else. You could also set up a simple test to see if the control can operate without ViewState -- making it OK to use in MVC -- and fall back on the hybrid approach.
It probably won't work. There is no viewstate, postbacks (in the Webforms sense) or page lifecycle which most commercial controls rely on in some fashion.

Resources