How to reuse the webforms mastepage in mvc 4 - asp.net

I have a problem how to reuse the webforms masterpage in mvc view. The project I am working on is an existing website where I have to change a single module to MVC. I have implemented successfully the routing logic but I am struggling with reusability of masterpages. I read this question here but it does not solve my problem because existing pages inherit from XClass which by itself inherits from System.Web.UI.Page. The same logic happens to be for mastepages. The reason is because the XClass makes a lot of logic in background - like dealing with session values and validating user data etc etc. Is there any way how can I use those masterpages within view in MVC? I tried the solution given in question above but while debugging there is a statement XClass xpage = Page as XClass in Page_Load event which gives me the value null and throws exception since in MVC we do not have Page_Load event etc... As I have read in other places they recommend to create a new masterpage for MVC but in my condition it would be very difficult. Any suggestion how to solve this problem ?
I am reading this article here but yet I am not clear how to implement it...
Here I have an image attached for masterpage in webforms.

Related

WebForms Transferring To MVC

I was wanting to ask the following about mvc to have a better understanding.
1 What is the difference between the way webforms and action controllers work.
2 How should one coming from a webforms background convert the page_load etc button clicks etc into mvc methods and events. Its this understanding that I am lacking.
3.How do i fill controls before I was used to setting the datasource but see allot of controls using the foreach on the front end is that really code separation?.
4 I will be developing a form designer in .net webforms I was used to using panels and loading controls but I see it will be neater in mvc by using partial views would that be my best course of action. I am a senior asp.net webforms developer with over ten years experience.
I have been watching plurisight videos but they center on using sql express not server.
A lot of help material you can get online about this topic.
Your first question about the way webforms and action controllers work -
In webform, you specify the code-behind file of your .aspx page and the code-behind file is the master of that page now. The browser hits the .aspx and the code behind file manages the work.
But in MVC, no view file is approached; the path is matched to the respective controller and action and the action handles it. Any controller can access the View of any other Controller. There are Shared Views which are common for every controller as well.
I strongly suggest you to read at this link and this codeproject article.
Some major points will be this :
You wont have the RAD (Rapid Application Development) environment i.e. the drag drop support for Controls, page viewer in case of Razor, etc.
You wont have the basic server controls Gridview, Repeater,etc in MVC. None of the controls in MVC are bind to any controller. You can neatly pick the desired elements using Javascript and play with them.
You get full control over the HTML
What I feel is that MVC is more flexible and jQuery-able as compared to Webforms
All the best!

MVC Custom Control?

I am trying to figure out how to use/create a custom control in ASP.NET MVC 2.
I created a custom control earlier and compiled it (ccontrol.dll), the control renders a div, textbox and a button + some javascript in order to post a comment on the website. It could be a static aspx page that i wanted to allow my visitors to add a comment to. I would then drag my control from the toolbar to the aspx page and run it, it would then render all the code needed on the webpage including fetching the data from a datasource and displaying that inside the div. The user could also just type in a comment and press the button to save it to the datasource.
Is this possible to convert to MVC 2? Any good tutorial that covers custom controls and MVC 2? (Ideally would be if the control could be made into a .dll file that i then could reuse on future webpages)
How do i write a custom control the mvc way? Any good tutorials on the topic?
You cannot design Custom Controls according the normal asp.net style because in Mvc there is no ViewState and there are no server side control events. Data are returned back to the server through a Model Binding process. The fact that rendering and filling data in are handled in separated pieces of code make difficult to implement complex server controls in Mvc.
However, I developed a theory, and also a toolset to make quite easily custom controls ina Mvc too in the full spirit of the Mvc paradigm i.e keeping separation of concerns between Views and Controllers. See My Codeplex project. There, you will find pointers to documentation and tutorials on my blog. If you need assistance feel free to contact me.
No it is not possible to use custom controls in ASP.NET MVC. you need to re-write in MVC way

Which ASP.NET Page event is the best place for this type of code?

I have a relatively simple page that will do most of its operations on the client side using Javascript or JQuery. However, initially I do need to retrieve some data from the DB server based on QueryString parameters.
I plan on passing this data in the form of a JSON string to the script by an old-fashioned ASP manner ( var severData = <%=MyPublicData %>) block where MyPublicData is defined in CodeBehind as:
Public string MyPublicData;
The question is, which event in the ASP.NET page lifecycle is the best for this? Page_Init ? Page_Load? Also, is it worth the effort to do this in ASP.NET MVC. I did look at this possiblity but it seemed a little too much for a simple page like this where I do more 90% of the work on the client. Any thoughts on this?
Page_Load is more appropriate, but either will work.
It's very difficult to say if MVC is more appropriate for you application than webforms without knowing more about the application. However, if you don't want to abstract away the traditional web model then I'd go with MVC.
This kind of simple property or field assignment can go anywhere in the lifecycle. For lack of any other reason, you might as well stick it in Page_Load, since that method is usually waiting for you in the code-behind anyway.
well, on the contrary, if it uses mostly javascript with jQuery, I would recommend you to use MVC. you will not have any problem with the ids for instance.
There are workaround to use jQuery with webforms, but it is never perfectly clean regarding selecting the DOM.
MVC : a single action method where you will retrieve your data (preferably from a small repository) and 1 view where you display your data with total control over your html elements.
and jQuery will just fit perfectly for your clientside work.
You can get either to work; you can write public variables/fields to the client in ASP.NET web forms, although MVC has an edge due to the way it renders the UI.
So for MyPublicData, you could assign it a value at any part of the lifecycle; you can assign it in code-behind to a label or something like that, or if it's JS markup you can write it out using Page.ClientScript.RegisterStartupScript or RegisterClientScriptBlock... so you have multiple options.
In MVC, you would assign the value in the controller and render it in the UI, or with JQuery you can do controller requests real easy with $.ajax.

ASP.NET: Webforms and MVC pattern

I have developed a webapplication in both ASP.NET MVC and ASP.NET Webforms and i'm wondering isn't Webforms following the rules of the MVC Pattern just the as ASP.NET MVC is?
I mean we have the .aspx file which holds the visual (HTML and JavaScript) and then the code behind file which controls the user interaction and data for the .aspx file. Then we could make a Repository lager for fetching and doing stuff with data.
Isn't this the same as following the rules of the MVC Pattern? View for visual, Controller for controlling user interaction and data for the Views and the Model fetching and doing stuff with data?
I know ASP.NET MVC and Webforms handles Postbacks and URL handeling differently, but im not comparing the two ASP.NET techniques, but the MVC Pattern in generele for the two techniques.
ASP.NET Webforms is definitely not following the MVC pattern.
In MVC you have three elements, the Model, the View, and the Controller.
In ASP.NET Webforms, you have your Page (codebehind and the markup are compiled into a single object, not seperate), and whatever data is being shown. You really have no controller. You make a requrest directly to the page rather than a controller and the page is responsible for both working with the data and rendering the page. Definitely not seperated like MVC would be.
Just the fact that you're not using the codebehind in your aspx page should give you a hint that there are some pretty substantial differences between MVC and webforms... You aren't using the codebehind, are you?
Not to mention how hard it is to test a Webforms page.... You are testing your code, right?
You can indeed use an MVC pattern with Web Forms, but all you are doing is adding additional classes or layers to the postback. The previous answers here are coming from a purist point of view that is more clear separations built into the technology. But, there is nothing preventing you from using different classes to represent the Model the View and Controller and only have your WebForms bind to the View. All of the communication is still done on postback, but it is still technically MVC and still the correct pattern to use.
Reference:
http://msdn.microsoft.com/en-us/magazine/ff955232.aspx

How would this be done in ASP.NET MVC?

I've got some experience with ASP.NET MVC, but only with a web application where the content of the master page doesn't change. I've been wondering how you would handle a site where for example the menu (which is on the master page) is loaded from a database. Where would you load this data?
In WebForms, you would load the menu in the code-behind of the master page, or have the menu as a user control and do the loading in the code-behind of that. But where is this done in MVC?
Do you create a class that inherits from Controller that you use for all your Controllers and let that load the menu on every Action invocation (I don't know if that's possible, but it seems likely)?
Or do you create a utility method that you call in every Action where you want it (because some Actions may only return a partial view that won't reload the menu), which - while not disastrous - seems a little tiresome.
Or would you sin against MVC and just load it in the master page's code-behind?
What's the best approach to this (of course not limited to my solutions)?
ActionFilters are used to intercept a request and do some processing. You could use them.
Is it a sin against the MVC pattern?
You are breaking the MVC pattern to some extent. But the higher level point is: does it provide much more value if you force yourself not to break it? I don't think that puts you in much trouble, so keep simplicity and maintainability in mind and choose the way you'd do it in your specific situation.
I would create a model type that the master page gets its data from. Then derive the page's model type from the MasterModel.
The controller populates an instance of the PageModel, and the master page picks it up.
(Alternatively the master page's model could be an interface implemented by the pages' models.)
NB. In MVC CTPs doing this required some intermediate classes to override type matching to get to base class data. I don't know if this has been fixed in RTM.
The ASP.NET MVC Futures assembly (more info here) contains an extension method that lets you do this in your master page:
<% Html.RenderAction<NavigationController>(c => c.Show()); %>
You need to reference the assembly and add the "Microsoft.Web.Mvc" namespace in your web.config file for this to work.

Resources