Can I convert a MVC app into a Razor/Blazor app with Windows Authentication?

Falanga, Rod, DOH 190 Reputation points
2024-08-08T20:52:35.4666667+00:00

In October of last year, I started working on a new ASP.NET MVC Core application, to convert an old WebForms application which no longer builds. I added an API project to the Visual Studio solution and scaffolded the database. But other than that, I've not done much to the app, due to many other urgent priorities.

Now, I'd like to get back to it. However, I'd also like to learn Blazor, which the original app isn't. And I also need to have this app use Windows Authentication. I'll be using .NET 8. And I'd also like to make the API project a minimal API project, which I have some experience at. Because of my need to make this work with Windows Authentication, I had been following the Microsoft Learn tutorial Integrate ASP.NET Core Razor components into ASP.NET Core apps.

There are several big changes I'd like to do with this solution. Even though it is 10 months old, it doesn't really have a lot of "work" into it, because of the other priorities I had to attend do. I'm thinking that perhaps I should just start over. However, before I pull the trigger on burning the current VS solution to the ground and start over from scratch, I thought I'd ask if it is possible to take what I have and convert it to a server-side and webassembly project with a minimal API project?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,573 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,584 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 54,401 Reputation points
    2024-08-08T21:04:32.59+00:00

    If you haven't started on the UX frontend then you can just create a new Blazor project and continue forward. The APIs can either be in the same project (you can move them) or a separate project depending on how complex you want to be.

    Blazor supports WinAuth as discussed here but pay careful attention to the warnings about how security works in Blazor. I don't use Blazor so I cannot comment too much on it but it seems like you're ultimately going to end up doing everything on the server side for rendering in order to get it to work. In that case Razor Components with MVC might be the easier approach.

    A word of warning on feature creep though. Migrating a web forms app to MVC is challenging in most cases because web forms were stateful and MVC is generally stateless. Add to that the codebehind of web forms compared to API calls of MVC and it gets even more complex. Now if you're going to throw learning new technology like Blazor on top of that, and probably new data access and other features like DI and ORMs, and you could spend a long time getting even something simple to work. Balance the need to get the app working with a reasonable chunk of learning otherwise you'll never finish.

    I would personally recommend that if you already know APIs and MVC then perhaps you should migrate the web forms app over to that. You could use Razor Components since they are not a big leap from MVC partial views that you might already understand. I would recommend you not go the Blazor route if you don't have any experience yet. Pick a different project, perhaps one already using MVC, to learn Blazor with so that you can focus on learning the details of Blazor without mixing in having to migrate the entire stack. If built correctly, changing a UX framework shouldn't be terribly hard once the backend and middle layers are modernized. So a phase 1 approach might be migrating to a modern MVC setup with separated layers. Then phase 2 is migrating just the UX to Blazor.


  2. Bruce (SqlWork.com) 65,576 Reputation points
    2024-08-12T15:57:55.47+00:00

    Razor pages is the replacement for webforms (though it does not implement the webforms tree and events). Blazor is MS response to react and has a similar architecture. (though I still prefer react).

    if you want to switch to Blazor, it would be much easier to create a new Blazor project, rather than convert your "old" code.

    you will need to decide between Blazor server and Blazor WASM. In server, Blazor code has access to server resources, WASM must make api calls like SPA. Blazor server takes more server resources and may have more UI latency.

    Windows authentication is supported when asp.net core is hosted by IIS. The default implementation creates a user principal that is available to Blazor server. Blazor WASM would call an api protected by windows authentication. The browser would handle passing the credentials on api calls. Best experience would be to authenticate the Blazor hosting page.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.