1

A site I'm programming is using ASP.NET MVC and WebForms, and I am stuck on how it was previously working (they lost files).

Within their ASPX file they have something along the lines of

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" Inherits="System.Web.Mvc.ViewPage<StudentRepo.UI.Mvc.Models.MenuSecurity>" %>


<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.RenderPartial("~/Menu.ascx", Model); %>


<div class="modal-footer">
                    <button id="btnCloseUpdateStudentType" type="button" class="btn btn-default" data-dismiss="modal" style="width:80px;">Close</button>               
                    <%if (Model.SecurityItems.Where(i => i.ViewPage == "Manage Student Types" && i.Update).Count() > 0)
                        {%>          
                    <button id="btnUpdateStudentType" type="button" class="btn btn-primary" rel="popover" data-placement="top" data-trigger="manual" style="width:80px;">Update</button>   
                    <%} %>                     
                </div>
</asp:Content>

But each time I debug and go to the page, the Html and Model Property are null, throwing the error "Object reference not set to an instance of an object." This is also true if I leave out the model and just RenderPartial on the ascx file.

How is the Html null when the Menu.ascx file is present in that path?

Where is expecting to get the Model from? The view of the Menu.ascx file? I don't understand the use of Model.SecurityItems.Where in the footer either.

I have tried and been stuck on this for weeks, and it seems they are doing it differently than most people since I have done a lot of research.

The views provided are either .aspx or ascx files, so I can't implement it with .cshtml files like I have seen others do.

Fraze
  • 11
  • 1

1 Answers1

1

A web site built around MVC is vastly different then the architecture of a asp.net webforms site.

So, while you CAN use aspx pages in a MVC site, it not a default, and you require a good deal of "tricks" to make this work.

The views provided are either .aspx or ascx files

Those are NOT called views, and you can't make such a statement any more then trying to say a glass of water and a dog are the same thing.

If you site has ZERO cshtml pages now, you do NOT have a MVC site, but you have a asp.net webforms site.

A aspx web page is a web forms page. There are no views in such pages.

You will have a aspx page with markup, and then a corresponding code behind page.

So, if you have a page called Fighters.aspx, then you will have (in most cases) a corresponding code behind page called Fighers.aspx.cs (or Fighters.aspx.vb if this is a vb.net site).

So, you see this in the project tree:

enter image description here

There are no cshtml pages in such a site, you can't add them, and they have nothing to do with a asp.net web forms site.

Different design, different architecture, and different operation.

so I can't implement it with .cshtml files

There are no such types of files in a webforms project.

And keep in mind there are two types of webform projects.

There is a asp.net webforms site

And then there is what is called a asp.net webforms application

The big difference is the lack of the word "application".

You can in about 5 seconds of time know the difference, and a webforms site will not have the "designer" files like a web application site has.

For a web site, you go file->open web site.

For a web site application, you file->open Project.

I should also point out that while MVC sites can be either .net core (the newer framework), asp.net web sites, or asp.net web site "applications" can ONLY be built using the .net framework, and NOT with .net core.

So, if was never any cshtml files in that project, then you do NOT have a MVC site. You can perhaps get some cshtml pages to work, but I don't think you should mess around trying to do that in this case.

As I noted, you CAN load and make aspx pages work in a MVC site, but you from what you provided don't have a "mvc" site.

However, if your posted code is from that project?

Then it looks like you may well have a MVC site, and it is loading + consuming asp.net "aspx" pages. While this is possible, it is somewhat convoluted approach here.

I would right click while viewing that aspx web page, and choose view code - you should see the code behind page for that aspx page.

We will likely require more details here, but that posted page of yours does not look correct, and it looks like a asp.net webforms master page, with some extra junk tossed in that does not really look correct.

If some pages in question REALLY do have this:

 <% Html.RenderPartial("~/Menu.ascx", Model); %>

Then OK, this means you do have some views, and do have some cshtml pages. However, in most cases, the reverse is done. So, if there are in fact some cshtml files in that project, then I stand corrected.

(You may well have a cshml page and it loads + uses a aspx page, or perhaps a ascx control).

I mean, when you open this project, (the sln file), does the site build + run when you hit f5?

Edit: The choice to create a MVC or webforms

Let's try this again. I stated that you don't have a project that is a "mix" of MVC and web forms.

You have this choice:

enter image description here

You can create a web forms project, or a MVC project, or a API project. That does not hint nor suggest nor imply that you can create a webforms + MVC project. Not even close!

Again, it suggests and hints NO such projects.

Let's click on that, and see the next panel:

enter image description here

So, you get to name the project, choose the .net framework.

However we still not have selected the project type.

Next page:

enter image description here

So you do not see any project that says MVC + web forms, you do not see ANY project that suggests a mix.

At this point in time, you can choose a empty project:

(a asp.net web forms project).

Or you can choose a Web Forms project - NOT MVC.

Or you can choose a MVC project - NOT web forms!

So, NO you DO NOT HAVE a MVC + web forms project template and none exist.

You can create a MVC project, or you can create a WebForms project.

As I stated, if you want to create a mvc project and consume aspx pages, then you can do this, and how you can do this is outlined here:

How to add .aspx pages to existing MVC 4 project?

However, as above shows, you WILL have created a MVC project for above to work.

Again, there is NOT a template choice that is some magic mix of MVC + web forms. Such projects and templates don't exist.

So, yes, you can consume aspx pages, but you not going to have some automatic, or auto "magic" template to do this, and you have to adopt code and it is a rather manual coding process that YOU the developer efforts here will have to make such a setup work.

IMHO it tends to be a rather bad idea, since you lose lots of MVC support stuff, and you lose lots of WebForms support stuff - you in fact wind up with the worst of both systems, and will be hampered using MVC, and you be hampered using WebForms. As the above link shows, this can be done, but you WILL NOT and DO NOT have a out of the box template system that is a MVC + WebForms combined project. You the developer will have to write some hack code and tricks to make this work.

halfer
  • 19,824
  • 17
  • 99
  • 186
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Thank you for the detailed response. I agree with what you're saying, and that's why I am just as confused. I created the project from scratch since I was only given a handful of files, I used the "ASP.NET Web Application (.NET Framework)" project template, targeting .NET framework 4.7.2. Within this I chose MVC and Web Forms, with Individual Account authentication. I chose the above because it seemed the most appropriate for the convoluted files I was given,which are: Two ASPX files used as views, One controller, Business logic file, and Two JS files – Fraze Jun 15 '23 at 13:06
  • Continuing my above comment. I chose MVC because even if it's not a true MVC, it looked like that is what they were doing/attempting previously. I am authenticating with individual accounts because certain items shouldn't be present unless a user has certain permissions. I typically develop ASP.NET apps with MVC, not WebForms so I am certainly confused with all this. – Fraze Jun 15 '23 at 13:11
  • and yes I am able to run the website and view the pages that are ASPX. The site loads just fine – Fraze Jun 15 '23 at 13:13
  • Hum, is there really a template that allows asp.net webforms and mvc pages in one? I did not know or realize such a template and setup existed. This is new to me. I'll have to check this out, since some detail is missing here that don't make sense. aspx pages are NOT views and as I stated, we can't confuse the 2 things. aspx pages are markup pages - not a class nor are they any kind of view in any way. – Albert D. Kallal Jun 15 '23 at 16:32
  • Yes I agree, and yes there is such thing. Please see here: https://imgur.com/a/na7veXb – Fraze Jun 15 '23 at 19:41
  • Created from VS 2022 Community edition. – Fraze Jun 15 '23 at 19:43
  • Rigth, but then drill down to the next page and the next page option and choice. You get Web Form OR you get MVC project. So, that top most menu ONLY gets you the choices and when you follow though, you eventually wind up with ONLY a choice to create a webforms project, or ONLY a choice to create a MVC project. There is no mix or hybrid. I mean, in that existing working project is the ONE existing cshtml page? (no, there is not, so that project you have it NOT mvc unless you telling me that some cshtml pages already exist? – Albert D. Kallal Jun 15 '23 at 20:11
  • Now, having stated above, you can adopt and consume aspx pages in a mvc project, but I would NOT do this unless that is what you already been give. How this works it outlined here: https://stackoverflow.com/questions/25385532/how-to-add-aspx-pages-to-existing-mvc-4-project – Albert D. Kallal Jun 15 '23 at 20:14
  • As I stated, UNLESS you REALLY have code that attempts RenderPartial, then I would not do this. However, if your posted code really exists and did use RenderPartial, then that does look like what's been attempted here. In other words, I would not try doing this unless that's what you already have. See the above link - it describes the approach that can be used here. – Albert D. Kallal Jun 15 '23 at 20:16
  • The first comment out MVC or Web Form project is not true. In my album you can see both being chosen in the second image. – Fraze Jun 16 '23 at 14:40
  • The first comment about* – Fraze Jun 16 '23 at 15:46
  • Let's try this again, nice and slow. There is NOT template choice for MVC + Webforms combined. When you choose the next page, you can choose MVC, or WebForms, you can NOT choose both, and no such template exists for creating such a hybrid type of application. See my screen shots and edit I added above. You can consume the aspx pages in that MVC project, but you be writing hacks and code to do this as per the link provided. – Albert D. Kallal Jun 17 '23 at 00:10