1

I have an ASP.NET application that was developed by "programmers". This application contains all things which you should not do:

  • Hardcoded settings
  • Copy/paste anywhere (code not re-used)
  • Make a lot of small SELECT requests to the DB for each row instead of doing JOIN
  • Model, view and controller in one function
  • Etc.

Now, my goal is not to throw everything away and start over, but I want to separate different aspects of the MVC of the application. I do not want to start a new MVC project, I would like to evolve the existing solution into something modular.

For the controller, there is no problem, I can create classes that will manage DB connections, send mails etc. On the other hand I do not know how to separate the view and the controller.

The problem that traditional ASP pages myPage.aspx have an associated file myPage.aspx.vb and in this vb file there are both view management part(page elements, like dropdowns) and also the Business part (controller) which is executed on the button click.

I thought about making a call to a myPageControl.vb class that will contain the business part from the file myPage.aspx.vb, which will call the Model (db, mail, other).

(View: myPage.aspx.vb) -> (Control: myPageControl.vb) -> (Model: Db.vb, Mail.vb)

The problem is: how should I do to modify the page content from the controller, for example change a list value or display a text on it. I have to make a call to the View (to the other direction) and pass by parameter the class MyPage (.asp.vb)

I have already tried to find an answer to my question, but I've found only answers taking about MVC projects. Does anyone have any idea how I should do it?

Constantin
  • 71
  • 5
  • 1
    This sounds like a WebForms app, not an MVC app. Unless I'm misreading this: `"pages myPage.aspx have an associated file myPage.aspx.vb"` – David Feb 16 '17 at 13:48
  • Incidentally - rewriting this as an MVC app is probably what you want to do – Milney Feb 16 '17 at 13:58
  • Note that WebForms and MVC can co-exist in the same project. You can gradually move functionality from one to the other. – David Feb 16 '17 at 14:07
  • I would suggest you do this later in the refactoring. Start by creating proper business object and data access. This will centralize the most important part (the business logic and the database queries). After that, look at creating MVC. – the_lotus Feb 16 '17 at 14:31
  • Controllers shouldn't be managing DB connections either - that should be in a data layer that your controller just talks to. – mason Feb 16 '17 at 14:45
  • @David My project is a ASP.NET Web Application. Which was converted from Web Site project using .Net Framework 2.0. You said that Web App and MVC can co-exist inside the same solution. - Yes, but not in the same project. Isn't it? Can I deploy both projects on IIS and share the same URL – Constantin Feb 16 '17 at 16:37
  • @Constantin: WebForms and MVC can co-exist in the same application. It's a problem many developers have faced when inheriting a WebForms application. Documentation and examples may be sparse, but findable. This looks like a good start: http://stackoverflow.com/questions/2203411/combine-asp-net-mvc-with-webforms Basically WebForms and MVC are both built on top of the same underlying components, and enabling them both works. – David Feb 16 '17 at 16:44
  • @David, I'm not sure if we are talking about the same things. I said that My project is a ASP.NET Web Application (not a Web Form) with .Net Framework 2.0. I use Visual Studio 2010. Is it compatible with MVC? – Constantin Feb 16 '17 at 17:15
  • @Constantin: What's the difference? If this isn't WebForms, then what *is* it? Is this perhaps one of those old project types that doesn't have a `.csproj` (or `.vbproj`) file at all? If that's the case then you're definitely going to want to convert the codebase into at least some kind of web application project (likely WebForms if that's the style being used) before putting a lot of work into it. If that isn't what you mean though then it's not really clear to me what you do mean. ASP.NET web applications generally come in the form of WebForms or MVC. – David Feb 16 '17 at 17:20
  • It is definitely a Web Forms app, that's really all there was back in the .NET 2.0 days. On a side note, why are you using Visual Studio 2010 in the year 2017? – mason Feb 16 '17 at 17:48

2 Answers2

0

Seperation of Concerns was one of the main problems with webforms, and one of the advantages of MVC. In my opinion the best you could probably do is seperate the business logic into classes like you are doing now so code could be reused throughout the application, but completely "seperating" everything may require rebuilding the application as an MVC app.

Rick james
  • 824
  • 1
  • 11
  • 30
-1

The only answer I've found to this is to have the controller send the "data to bind to" to the page as XML. then all the page has is its page_load of course, and then a method to receive the XML and update itself from it. You can use smart naming structures so that you can do reflection and autobind from the xml to page elements.

Then on an action, have the page generate an xml of all the elements on it and their values, and send that through a "ProcessAction" method that determines the correct controller and calls the right method on the controller.

But as suggested, doing it over as an MVC project probably makes the most sense if that's the pattern you are going for. What I suggested works, but it will be as much or more work than just starting from scratch with MVC. Besides, remember that "support" for web forms is disappearing soon. It's not in (and won't be in) .NetCore, so it's days are numbered.

Eric Lizotte
  • 224
  • 1
  • 7