ā MVC within ASP.NET Web App
appologies upfront if my terminolgy is inccorect, im new to c#. i have my program build and it allows me to create exam scores and save them on one page. i have another page which uses MVC and is supposed to display a grade report based on the scores. the button on my index page for viewing the grade report seems to be doing nothing though. ive included all the code snippets i believe are related but if im missing something please let me know.
91 Replies
BlazeBin - bwmvyylrqzql
A tool for sharing your source code with the world!
You seem to be doing double routing (attributes and MapControllerRoute).. which isnt the issue but it makes the code harder to follow.
Have you tried setting breakpoints to see if your routes work?
can you reach the
Report/GradeReport
route at all?No I cannot even if I try to manually navigate to it I just get the page isn't working error "local host didn't send any data" ERR_EMPTY_RESPONSE
ok so that means your routing isnt set up correctly
Hmm so is it because of the double routing that you mentioned. Either way to me it looked as If everything was set correct, I mean obviously not but I'm lost then
and all the other routes work?
Yes all other pages the exam, student and teachers pages work and the buttons from the home page properly direct. And that's why it's weird because they are set up the same as this but I'm wondering if it's because those are razor pages and this is MVC
Not sure if that means anything
But that's the only difference I know of
well, your report route doesnt have an associated area, which all the other route do
so thats different
oh, yes thats a huge difference
That's why it doesn't have the area I thought it uses controller only but maybe I'm just way off
looking at your code, you dont seem to register the controller with DI either
actually, this is even using a old-school
Startup.cs
file
what version of .NET is this?6
okay
not too ancient then š
I chose long term support thought that was best rather than 7
you'll need to do
services.AddControllersWithViews();
no idea why you'd think that, but sureMy startupfile wasn't included in the template I had so I made it from scratch that's probably why it looks different maybe
Oh I just thought because it said long term support that was the ideal framework
thats because since .net 6 (or was it 5?) and forward we dont use
Startup.cs
anymore
this is the program.cs for a brand new .net 7 MVC app:
but its fine, the startup class thing still works
its just not the default template or how the ASP team wants you to do things anymoreI just realized I also have a program.cs file and I just uploaded that to the blazebin, I'm use to seeing the startup that I completely looked past the program file and didn't touch it
you'll need to paste the new url
Oh my bad okay
BlazeBin - pghxcavaehkb
A tool for sharing your source code with the world!
wait what
oooh
jesus
okay
so, you kinda butchered this app š
Lovely
Yeah all of that in the program was auto generated when I used a template and I didn't even remember that until you just said this
your
Startup.cs
is only used for startup.ConfigureServices(builder.Services);
it never uses the Configure
method, so all that is dead code
that means all your endpoint mapping and stuff is simply not loaded
so what I would do if I were you is to just move all your configureServices
stuff into program.cs, using the new builder.Services.Add...
format
and for the "configure", leave it alone for now and just add a app.MapControllers();
near app.MapRazorPages();
So take it all from startup and add it to the program so how much of the default created program is necessary to keep
.. ?
keep all thats in there now
just move your configure services stuff over, and add
app.MapControllers()
leave your startup.Configure stuff alone, I dont think you need it (its not being used atm)Oh gotcha sorry for my ignorance
So then after doing that the MVC should work as expected
hopefully
you'll also need
builder.Services.AddControllersWithViews();
I stepped away from computer for a bit so I'll have to test it all in a bit so I'll update the thread if it still doesn't work but thanks so much for all the help it really is greatly appreciated
glad you posted your Program.cs so we found that half of your startup wasnt even being used š
Lmao well I'm glad you even mentioned it because I would of been stuck for quite sometime not even realizing that was more than likely the problem all along
So for future reference go with net 7 and then I don't ever need a startup, in regards to future programs
i updated my startup and program files to this https://paste.mod.gg/faemswckvxnq/0 but i think it just broke my application. im going to upload a screen recording of before and after.
BlazeBin - faemswckvxnq
A tool for sharing your source code with the world!
and the view grade report still didnt work?
@Pobiega
well yeah
you forgot to move over a lot of important stuff
you didnt move that, for example
Cool š okay what all else did I forget ?
š
So if I move over those two then it should work and other than that the updated code looks good?
yeah sorta
I mean I'm just guessing here, I have literally never made anything with razorpages
but it "should" work
Gotcha well the best way to learn anyway is trial and error and error and error š
yep!
Anyways thanks so much I actually can't stay awake any longer so I'll attempt this in the morning and go from there , fingers crossed
gl
In the new projects i think startup.cs is removed and everything is now in program.cs
@Iron what do you mean? So I need to scrap the startup and add it all to program?
@Pobiega I implemented the changes as you mentioned and the first 3 buttons work again but the view reports button still does nothing
you've already done that
That's what I thought
the startup is at this point 100% unused
can you upload your code to github or something?
The entire project you mean?
sure, or a smaller reproduction of your error
your call
Is GitHub easier or just a zipped up file of the project? Which I already have done
we're not too fond of zips here in general as they can contain whatever
while a github repo I can check whats in it before I clone š
Gotcha that's understandable and I could be mistaken but I vaguely remember a few years back there was a website you could fully check a zip file for anything malicious before even opening it. Don't even know if that was actually a thing but GitHub is fine
@Duchess still alive?
It's uploading to github sorry I'll post link when done
for 30 minutes? š
Apologies I wasn't able to do it right away got a fussy baby had to help first
Ah
I remember those days. Happily a few years behind me at this point š
Yeah I've got 2 under 1 1/2 so I've still got a ways š
GitHub
GitHub - GummyBair/FinalProject: Final Project attempted build
Final Project attempted build. Contribute to GummyBair/FinalProject development by creating an account on GitHub.
well, I've got razorpages + mvc working in the same project now
Like you tried to get one working on your own you mean? Or mine
my own
Gotcha
oh boi
That doesn't sound good
well
you're gonna be a bit upset with yourself for how easy of a fix this is š
I'm upset with myself for not catching it earlier
Well can't be as bad as when I couldn't get the exams to save and it was simply making the students nullable in the model a single character fixed that and it took me two days to notice
GitHub
use controller and action routing helpers for MVC routes by pobiega...
This fixes the link not working.
I made a pull request š
No no i was just pointing it out hehe
@Pobiega omfg š
Oh gotcha
so yeah, tag helpers actually look at what exists in your codebase
your tag helper couldnt find a page called "Report/GradeReport"
because there was no such page š
there was a controller with an action however, so we just change to that tag helper and presto, it works
You're right I'm upset with myself it was that easy, guess I didn't have enough coffee that night and tired ass self was my downfall
Well thank you very much
well, we did discover and fix a lot of other issues
the whole startup thing, for example
(please delete that file)
True which I had no idea about and I'll definitely do that
Won't be making that mistake ever again
also, can I recommend "File scoped namespaces"?
I wasn't even aware that was a thing would that apply to the entire project anywhere the latter was used
oh, its not the entire project, its per file
but so are your current namespace declarations š
you should have a "quick action" to switch
and that can be applied to entire project
looks like this in Rider, but very similar in VS
Ah I see
Slightly off topic question I see a lot of people recommend other IDE's than visual studio and it looks like even you don't use it, should I switch to something else going forward
VS is great. I just personally find Rider to be... even better.
But Rider isn't free (unless you are a student), so its not always feasible to use for beginners.
To me, the cost isn't an issue if it saves me an hour or two per year, which I'm sure it does.
Gotcha well I don't mind paying for something if I'm going to use it and it makes things easier/better/more streamline
I fully agree with that
Given the name of your project, are you a student perhaps?
Because $freerider is a thing
There are two ways to get Rider for free:
Being a student: https://www.jetbrains.com/community/education/#students
Using it for an OSS project: https://www.jetbrains.com/community/opensource/#support
Yes I am actually
then go to that top link and see if you qualify.
worst case, you try the trial and buy it later if you want or find it to be worth it
but honestly, VS really is great.
I'll definitely do that then thanks again
as long as you're not using VS Code, you're fine
VS Code is also great, but not for C# specifically
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.