Giter VIP home page Giter VIP logo

icnocop / blazormvc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jeffreypalermo/blazormvc

0.0 0.0 0.0 386 KB

Opt-in Model-View-Controller framework for Blazor. Keep UI logic away from layout and formatting. Enable decoupled and dynamically composed screens along with broadcasted UI events. From a founder of the ASP.NET MvcContrib library.

License: Apache License 2.0

C# 50.51% HTML 31.04% CSS 18.45%

blazormvc's Introduction

Blazor MVC

Add C# MVC capability to any Blazor application

Blazor MVC is a C# Project that enables the ability to use the MVC framework with Blazor applications. Developers that have experience with this framework in the past can have familiar structure and quickly move into Blazor.

Features

  • Compatibility with .NET 3, .NET 5, and .NET 6
  • Compatibility with both Blazor Server applications and Blazor WebAssembly
  • Example projects on how to implement Blazor MVC
  • Create Controllers and Models for each of your views by extending prebuild classes

Installation

This section can be used for either Blazor web server or WebAssembly applications. After this section navigate to your desired project type and see the different implementations.

Note: You can find working examples of one for each .NET version and for each type of Blazor project (Blazor Server or Blazor WebAssembly)

  • The first step to add BlazorMVC to your project is to download the BlazorMvc project from (https://github.com/jeffreypalermo/blazormvc).

  • Next, create a New Project or open an Existing Project using the Blazor framework

  • Navigate to the root folder of the desired solution within File Explorer and copy the project folder BlazorMvc from the downloaded project

Note: You can add a new project within the downloaded solution of BlazorMvc.

  • Next, open the project in Visual Studio

  • Right click on the solution, hover over Add, and select Existing Project....

    • Navigate to the folder of your current project and select the .csproj file of the BlazorMvc project that you copied over
  • Once BlazorMvc has been added to the solution, next add the project as a build dependencies within your desired project.

    • Right click on the blazor project, hover over Build Dependencies, and select Project Dependencies

      • Select BlazorMvc and click Ok
  • Then you need to edit the project file by double clicking your project in Visual Studios.

  • Within the newly opened .csproj copy and paste the below code within the tag.

<!--Add Reference to the BlavorMvc Project within the Solution--> 
    <ItemGroup> 
        <ProjectReference Include="..\BlazorMvc\BlazorMvc.csproj" /> 
    </ItemGroup>  

Above are the steps that are required to get started using BlazorMvc within your Blazor application. Below are the corresponding steps that are needed for both different types of applications to get them to run.

Note: To use BlazorMvc in components you need to make sure you using Palermo.BlazorMvc;

Blazor WebAssembly Application

For the Blazor WebAssembly application you need to edit a couple files in order to allow the BlazorMvc library to be properly implemented

  • Program.cs
using Microsoft.Extensions.Logging.Abstractions; 
using Palermo.BlazorMvc; 
  
var builder = WebAssemblyHostBuilder.CreateDefault(args); 
builder.Services.AddScoped<IUiBus>(provider => new MvcBus(NullLogger<MvcBus>.Instance)); 

// AppController is the class that we are creating next. You can use any naming convention. You will need to add the using statement to gain access to this class within this file 
builder.RootComponents.Add<AppController>("#app"); 
  • AppController.cs - Create a new class
using Microsoft.AspNetCore.Components.Rendering; 
using Palermo.BlazorMvc; 

// Change namespace to match yours 
namespace Sample.WebAssemblyNet6 
{ 
    // AppView is the default app view. If you created a new Blazor application this class will be names App. We renamed it to AppView 
    public class AppController : ControllerComponentBase<AppView> 
    { 
        protected override void BuildRenderTree(RenderTreeBuilder builder) 
        { 
            base.BuildRenderTree(builder); 
        } 
    } 
} 
  • AppView.razor- This is the App.razor file that was renamed
@inherits Palermo.BlazorMvc.ViewComponentBase 

<Router AppAssembly="@typeof(Program).Assembly"> 
    <Found Context="routeData"> 
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayoutController)"> 
        </RouteView> 
    </Found> 
    <NotFound> 
        <LayoutView Layout="@typeof(MainLayoutController)"> 
            <p>Sorry, there's nothing at this address.</p> 
        </LayoutView> 
    </NotFound> 
</Router> 

Blazor Server Aplication

  • Program.cs
using Microsoft.Extensions.Logging.Abstractions; 
using Palermo.BlazorMvc; 

var builder = WebApplication.CreateBuilder(args); 
builder.Services.AddScoped<IUiBus>(provider => new MvcBus(NullLogger<MvcBus>.Instance)); 

// Add services to the container. 
builder.Services.AddRazorPages(); 
builder.Services.AddServerSideBlazor(); 
builder.Services.AddScoped<AppController>(); 
builder.Services.AddScoped<WeatherForecastService>(); 
  • AppController.cs - Create a new class
using Microsoft.AspNetCore.Components.Rendering; 
using Palermo.BlazorMvc; 

// Change namespace to match yours 
namespace Sample.BlazorServerNet6 
{ 
    // AppView is the default app view. If you created a new Blazor application this class will be names App. We renamed it to AppView 
    public class AppController : ControllerComponentBase<AppView> 
    { 
        protected override void BuildRenderTree(RenderTreeBuilder builder) 
        { 
            base.BuildRenderTree(builder); 
        } 
    } 
} 
  • AppView.razor- This is the App.razor file that was renamed
@inherits Palermo.BlazorMvc.ViewComponentBase 

<Router AppAssembly="@typeof(Program).Assembly"> 
    <Found Context="routeData"> 
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayoutController)"> 
        </RouteView> 
    </Found> 
    <NotFound> 
        <LayoutView Layout="@typeof(MainLayoutController)"> 
            <p>Sorry, there's nothing at this address.</p> 
        </LayoutView> 
    </NotFound> 
</Router> 
  • _Host.cshtml
<component type="typeof(AppController)" render-mode="ServerPrerendered" /> 

blazormvc's People

Contributors

jeffreypalermo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.