C
C#ā€¢15mo ago
Duchess

ā” 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
Duchess
DuchessOPā€¢15mo ago
BlazeBin - bwmvyylrqzql
A tool for sharing your source code with the world!
Pobiega
Pobiegaā€¢15mo ago
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?
Duchess
DuchessOPā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
ok so that means your routing isnt set up correctly
Duchess
DuchessOPā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
and all the other routes work?
Duchess
DuchessOPā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
well, your report route doesnt have an associated area, which all the other route do so thats different oh, yes thats a huge difference
Duchess
DuchessOPā€¢15mo ago
That's why it doesn't have the area I thought it uses controller only but maybe I'm just way off
Pobiega
Pobiegaā€¢15mo ago
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?
Duchess
DuchessOPā€¢15mo ago
6
Pobiega
Pobiegaā€¢15mo ago
okay not too ancient then šŸ™‚
Duchess
DuchessOPā€¢15mo ago
I chose long term support thought that was best rather than 7
Pobiega
Pobiegaā€¢15mo ago
you'll need to do services.AddControllersWithViews(); no idea why you'd think that, but sure
Duchess
DuchessOPā€¢15mo ago
My 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
Pobiega
Pobiegaā€¢15mo ago
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:
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
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 anymore
Duchess
DuchessOPā€¢15mo ago
I 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
Pobiega
Pobiegaā€¢15mo ago
you'll need to paste the new url
Duchess
DuchessOPā€¢15mo ago
Oh my bad okay
Duchess
DuchessOPā€¢15mo ago
BlazeBin - pghxcavaehkb
A tool for sharing your source code with the world!
Pobiega
Pobiegaā€¢15mo ago
wait what oooh jesus okay so, you kinda butchered this app šŸ˜„
Duchess
DuchessOPā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
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();
Duchess
DuchessOPā€¢15mo ago
So take it all from startup and add it to the program so how much of the default created program is necessary to keep
Pobiega
Pobiegaā€¢15mo ago
.. ? 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)
Duchess
DuchessOPā€¢15mo ago
Oh gotcha sorry for my ignorance So then after doing that the MVC should work as expected
Pobiega
Pobiegaā€¢15mo ago
hopefully you'll also need builder.Services.AddControllersWithViews();
Duchess
DuchessOPā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
glad you posted your Program.cs so we found that half of your startup wasnt even being used šŸ˜„
Duchess
DuchessOPā€¢15mo ago
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
Duchess
DuchessOPā€¢15mo ago
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!
Duchess
DuchessOPā€¢15mo ago
Duchess
DuchessOPā€¢15mo ago
Duchess
DuchessOPā€¢15mo ago
and the view grade report still didnt work? @Pobiega
Pobiega
Pobiegaā€¢15mo ago
well yeah you forgot to move over a lot of important stuff
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
you didnt move that, for example
Duchess
DuchessOPā€¢15mo ago
Cool šŸ˜’ okay what all else did I forget ?
Pobiega
Pobiegaā€¢15mo ago
else
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseDeveloperExceptionPage();
}
šŸ™‚
Duchess
DuchessOPā€¢15mo ago
So if I move over those two then it should work and other than that the updated code looks good?
Pobiega
Pobiegaā€¢15mo ago
yeah sorta I mean I'm just guessing here, I have literally never made anything with razorpages but it "should" work
Duchess
DuchessOPā€¢15mo ago
Gotcha well the best way to learn anyway is trial and error and error and error šŸ˜‚
Pobiega
Pobiegaā€¢15mo ago
yep!
Duchess
DuchessOPā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
gl
Iron
Ironā€¢15mo ago
In the new projects i think startup.cs is removed and everything is now in program.cs
Duchess
DuchessOPā€¢15mo ago
@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
Pobiega
Pobiegaā€¢15mo ago
you've already done that
Duchess
DuchessOPā€¢15mo ago
That's what I thought
Pobiega
Pobiegaā€¢15mo ago
the startup is at this point 100% unused can you upload your code to github or something?
Duchess
DuchessOPā€¢15mo ago
The entire project you mean?
Pobiega
Pobiegaā€¢15mo ago
sure, or a smaller reproduction of your error your call
Duchess
DuchessOPā€¢15mo ago
Is GitHub easier or just a zipped up file of the project? Which I already have done
Pobiega
Pobiegaā€¢15mo ago
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 šŸ™‚
Duchess
DuchessOPā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
@Duchess still alive?
Duchess
DuchessOPā€¢15mo ago
It's uploading to github sorry I'll post link when done
Pobiega
Pobiegaā€¢15mo ago
for 30 minutes? šŸ˜„
Duchess
DuchessOPā€¢15mo ago
Apologies I wasn't able to do it right away got a fussy baby had to help first
Pobiega
Pobiegaā€¢15mo ago
Ah I remember those days. Happily a few years behind me at this point šŸ™‚
Duchess
DuchessOPā€¢15mo ago
Yeah I've got 2 under 1 1/2 so I've still got a ways šŸ˜‚
Duchess
DuchessOPā€¢15mo ago
GitHub
GitHub - GummyBair/FinalProject: Final Project attempted build
Final Project attempted build. Contribute to GummyBair/FinalProject development by creating an account on GitHub.
Pobiega
Pobiegaā€¢15mo ago
well, I've got razorpages + mvc working in the same project now
Duchess
DuchessOPā€¢15mo ago
Like you tried to get one working on your own you mean? Or mine
Pobiega
Pobiegaā€¢15mo ago
my own
Duchess
DuchessOPā€¢15mo ago
Gotcha
Pobiega
Pobiegaā€¢15mo ago
oh boi
Duchess
DuchessOPā€¢15mo ago
That doesn't sound good
Pobiega
Pobiegaā€¢15mo ago
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
Duchess
DuchessOPā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
I made a pull request šŸ™‚
Iron
Ironā€¢15mo ago
No no i was just pointing it out hehe
Duchess
DuchessOPā€¢15mo ago
@Pobiega omfg šŸ˜’ Oh gotcha
Pobiega
Pobiegaā€¢15mo ago
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
Duchess
DuchessOPā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
well, we did discover and fix a lot of other issues the whole startup thing, for example (please delete that file)
Duchess
DuchessOPā€¢15mo ago
True which I had no idea about and I'll definitely do that Won't be making that mistake ever again
Pobiega
Pobiegaā€¢15mo ago
also, can I recommend "File scoped namespaces"?
namespace FinalProject_2202331;
//instead of..
namespace FinalProject_2202331
{
// your code here
}
namespace FinalProject_2202331;
//instead of..
namespace FinalProject_2202331
{
// your code here
}
Duchess
DuchessOPā€¢15mo ago
I wasn't even aware that was a thing would that apply to the entire project anywhere the latter was used
Pobiega
Pobiegaā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
Pobiega
Pobiegaā€¢15mo ago
looks like this in Rider, but very similar in VS
Duchess
DuchessOPā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
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.
Duchess
DuchessOPā€¢15mo ago
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
Pobiega
Pobiegaā€¢15mo ago
Given the name of your project, are you a student perhaps? Because $freerider is a thing
MODiX
MODiXā€¢15mo ago
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
Duchess
DuchessOPā€¢15mo ago
Yes I am actually
Pobiega
Pobiegaā€¢15mo ago
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.
Duchess
DuchessOPā€¢15mo ago
I'll definitely do that then thanks again
Pobiega
Pobiegaā€¢15mo ago
as long as you're not using VS Code, you're fine VS Code is also great, but not for C# specifically
Accord
Accordā€¢15mo ago
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.
Want results from more Discord servers?
Add your server